简体   繁体   中英

python: Plotting data from multiple netCDF files

long story short, I am plotting climate data from a netCDF file. My only problem, I have to plot data from tens of these files, each having over a hundred data points. Luckily, they are all identically formatted, and their names are in rising order (for example: file1.nc, file2.nc...). This is my code (unfinished as I have to change the markers and colors of the markers):

Anyways, I want to plot more than that one file (about 20 to begin with). Is there a way to do that? Also, if you guys have an idea how how to set up the colorbar based on variable 'data' that would be great.

Thanks!

Make an empty array :

data =[] 

Make a list of filenames :

flist=["001.dat","002.dat",...] 

then iterate through that list:

for fn in flist: 
        data.append( netcdf_file(fn,'r'))

Now you can refer to your data sets like:

data[0]
data[1]

etc.

at the least, plt.savefig("some unique name") means you can generate them in a loop without having to save the plots/close them individually.

I also suggest getting comfortable with the object oriented interface :

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
map = Basemap(projection='robin',lon_0=0,resolution='l',ax=ax)
#keep all your code
cs = map.scatter(x,y,data)
fig.savefig("{}".format(some unique identifier))

Eta: And you can find all the files using glob if they're in the same folder:

import glob
filelist = glob.glob('/Users/epsuser/Dropbox/Argo/Data/*.nc')
for fl in filelist:
    ncfile = netcdf_file(fname,'r')
    #the rest of your reading code
    fig = plt.figure()
    #etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM