该脚本在Python2.x中工作,并在一次运行中生成多个绘图。现在,在Python3.x中,它只生成一个p

2024-09-27 07:23:19 发布

您现在位置:Python中文网/ 问答频道 /正文

大约一年前,我在Python2.x中写了这篇文章。我已经切换到3.x了,我没有收到任何错误,但是如上所述,只输出了一个绘图,第一个stdev绘图。如果我在Spyder中使用#%%在…###mean plot##下生成代码。。。进入自己的单元格并运行单元格,它将绘制该绘图。如果我切换mean和stdev代码的顺序,那么它只会使第一个出现。我有很多类似的脚本,它们都有相同的问题

是压痕吗?我一直在玩弄它,但没有用

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 28 09:16:29 2017

@author: kmiranda
"""

import numpy as np 
import matplotlib.pyplot as plt
import mpl_toolkits.basemap as bm
##from ens_function import ens_data, grd_data
from scipy.io import netcdf

for date in np.arange(3,16):
    mem=0o01

    DTG = '201501%2.2d00' % (date)
    nfdir='/u/prob/coupled/rowley/pertobs/kmiranda/scratch/gom/ensemble/post'
    ncfn=nfdir + '/ncom_relo_gom_' + DTG + '.%3.3d'%mem + '.nc'
    nc1 = netcdf.netcdf_file(ncfn, 'r')
    lat = nc1.variables['lat'][:]
    lon = nc1.variables['lon'][:]
    dep = nc1.variables['depth'][:]
    tau = nc1.variables['tau'][:]  ## added this. it's probably not right
    water_temp = nc1.variables['water_temp']
    t0 = np.squeeze(water_temp[1,:,:,:])*1. ##I changed the 0 to 1 here
    t0[np.where(t0 == -30000)]=np.nan



    bbox=[262.,281.,18.,31.]
    ma = bm.Basemap(projection='merc',llcrnrlat=bbox[2],urcrnrlat=bbox[3], \
                llcrnrlon=bbox[0],urcrnrlon=bbox[1],lat_ts=0,resolution='i')



    s1 = np.zeros(t0.shape)
    s2 = np.zeros(t0.shape)



    #del lat, lon, dep, water_temp, t0
    #nc1.close()

    for i in np.arange(1,33):

        DTG = '201501%2.2d00' % (date)
        nfdir='/u/prob/coupled/rowley/pertobs/kmiranda/scratch/gom/ensemble/post'
        ncfn=nfdir + '/ncom_relo_gom_' + DTG + '.%3.3d' %i + '.nc'

        nc1 = netcdf.netcdf_file(ncfn, 'r')
        water_temp = nc1.variables['water_temp']
        t0 = np.squeeze(water_temp[1,:,:,:])*1. ##I changed the 0 to 1 here
        t0[np.where(t0 == -30000)]=np.nan
        temp = np.ma.masked_invalid(np.squeeze(t0)*water_temp.scale_factor + water_temp.add_offset)
        temp.min()
        n = i  

        s1 = s1 + np.power(temp,2)
        s2 = s2 + temp
        s2.min()



    for k in np.arange(0,21,20):    
        std = np.sqrt((((n * s1)-(np.power(s2,2)))/(n**2)))
        mean = s2/n
        mean = np.squeeze(mean[k, :, :])
        std = np.squeeze(std[k, :, :])



        ################ stdev plot ####################

        x, y = np.meshgrid(lon, lat)
        p1 = ma.pcolormesh(x, y, std, shading = 'flat', \
        cmap = plt.cm.jet, latlon = True, vmin=0, vmax=0.8)

        c = ma.colorbar()
        #degree = u'\xb0'
        #c.set_label("C%s" % (degree))
        #m.colorbar(p1)
        ma.drawcoastlines()
        ma.fillcontinents(color='#EDE0BE')
        ma.drawparallels(np.arange(20., 30., 5.))
        ma.drawmeridians(np.arange(270., 280., 5.))
        ma.drawcoastlines(linewidth=0.25)
        ma.drawcountries(linewidth=0.25)
        plt.title('Std Temperature %s %dm' % (DTG, dep[k]))
        fig_name =nfdir + '/temp_std_z%06.1f' % dep[k] + '_ncom_relo_gom_' + DTG + '.%3.3d' % int(tau[1]) + '.png'
        plt.savefig(fig_name , dpi=200)    
        plt.show()
        plt.clf()

        ########### mean plot ###################
        if k == 0:
            vmin =10
            vmax = 30
        else:
            vmin = 10
            vmax = 28


        p2 = ma.pcolormesh(x, y, mean, shading = 'flat', \
        cmap=plt.cm.jet, latlon= True, vmin=vmin, vmax=vmax)

        c2 = plt.colorbar()
        degree = u'\xb0'
        c2.set_label("C%s" % (degree))

        ma.drawcoastlines()
        ma.fillcontinents(color='#EDE0BE')
        ma.drawparallels(np.arange(20., 30., 5.))
        ma.drawmeridians(np.arange(270., 280., 5.))
        ma.drawcoastlines(linewidth=0.25)
        ma.drawcountries(linewidth=0.25)
        plt.title('Mean Temperature %s %dm' % (DTG, dep[k]))
        fig_name =nfdir + '/temp_avg_z%06.1f' % dep[k] + '_ncom_relo_gom_' + DTG + '.%3.3d' % int(tau[1]) + '.png'
        plt.savefig(fig_name , dpi=200)  
        plt.show()
        plt.clf()

Tags: nppltvariablesmeantemps2waterma

热门问题