使用 matplotlib.basemap 绘制地图时出现问题
Problem with plotting map using matplotlib.basemap
这是我的 Python 代码。它工作过一次,但在我改变了一些东西之后,我不记得我改变了什么,现在 returns 这个错误:
raise RuntimeError("Can not put single artist in " RuntimeError: Can
not put single artist in more than one figure
有关此问题的任何帮助
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy.ma as ma
import netCDF4
import os
import datetime
mydate='20181110' #(put today date)
myhour = '00' # in UTC timezone
url='http://nomads.ncep.noaa.gov:9090/dods/hrrr/hrrr%s/hrrr_sfc.t%sz' %(mydate,myhour)
print url
# make sure folder exists
folder_path = 'pcptype/%s/%sz' %(mydate,myhour)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Read data
t = 10
file = netCDF4.Dataset(url)
lat = file.variables['lat'][:]
lon = file.variables['lon'][:]
time = file.variables['time'][:]
rad_ref = file.variables['refcclm'][t,:,:]
#### plotting data
m=Basemap(projection='merc',lat_ts=0,llcrnrlon=-81.0, \
urcrnrlon=-66,llcrnrlat=41.0,urcrnrlat=49.0, resolution='i')
bounds_rain = [0,5,10,15,20,25,30,35,40,100]
bounds_ref = [10,25,30,35,40,100] cmap=mpl.colors.ListedColormap(['#99CCFF','#0099FF','#00FF66','#00CC00','#009900','#006600','#FFFF33','#FFCC00','#FF9900','#FF6600','#FF0000','#FF0299','#9933CC','#660099'])
xx,yy = np.meshgrid(lon,lat)
x,y =m(xx,yy)
m.drawstates()
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary()
norm_ref = mpl.colors.BoundaryNorm(bounds_ref, cmap.N)
first_date = datetime.datetime(1,1,1,0,0,0)
init_datetime = first_date+ datetime.timedelta(time[0])
#for t in range(1, len(time)):
print 'processing time %i ' %t
fct_datetime = first_date + datetime.timedelta(time[t])
fig_name = 'pcp_type_%s' %(fct_datetime.strftime("%Y%m%d_%H00"))
print fig_name
fig=plt.figure(figsize=(10, 10))
map1=m.contourf(x, y, rad_ref,len(bounds_ref),norm=norm_ref,cmap=cmap,levels=bounds_rain)
plt.title("RADAR | Init : %s Valid: %s" \
%(init_datetime.strftime("%HZ %Y-%m-%d"),fct_datetime.strftime("%HZ %Y-%m-%d")),loc='left')
plt.savefig("%s/%s.png" %(folder_path,fig_name), dpi=200,bbox_inches='tight' )
情节的顺序必须正确完成。 Filled-contour 是光栅类型,必须先绘制。然后,可以在上面绘制所有矢量数据。这是显示情节序列的代码片段:
# ...
# Raster data must be plotted first
map1 = m.contourf(x, y, rad_ref, len(bounds_ref), norm=norm_ref, cmap=cmap,
levels=bounds_rain)
# then, these line/vector data are plotted
m.drawstates()
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary()
# additionally, plot useful colorbar
plt.colorbar(shrink=0.55) # draw colorbar
# ...
结果图:
这是我的 Python 代码。它工作过一次,但在我改变了一些东西之后,我不记得我改变了什么,现在 returns 这个错误:
raise RuntimeError("Can not put single artist in " RuntimeError: Can not put single artist in more than one figure
有关此问题的任何帮助
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy.ma as ma
import netCDF4
import os
import datetime
mydate='20181110' #(put today date)
myhour = '00' # in UTC timezone
url='http://nomads.ncep.noaa.gov:9090/dods/hrrr/hrrr%s/hrrr_sfc.t%sz' %(mydate,myhour)
print url
# make sure folder exists
folder_path = 'pcptype/%s/%sz' %(mydate,myhour)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Read data
t = 10
file = netCDF4.Dataset(url)
lat = file.variables['lat'][:]
lon = file.variables['lon'][:]
time = file.variables['time'][:]
rad_ref = file.variables['refcclm'][t,:,:]
#### plotting data
m=Basemap(projection='merc',lat_ts=0,llcrnrlon=-81.0, \
urcrnrlon=-66,llcrnrlat=41.0,urcrnrlat=49.0, resolution='i')
bounds_rain = [0,5,10,15,20,25,30,35,40,100]
bounds_ref = [10,25,30,35,40,100] cmap=mpl.colors.ListedColormap(['#99CCFF','#0099FF','#00FF66','#00CC00','#009900','#006600','#FFFF33','#FFCC00','#FF9900','#FF6600','#FF0000','#FF0299','#9933CC','#660099'])
xx,yy = np.meshgrid(lon,lat)
x,y =m(xx,yy)
m.drawstates()
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary()
norm_ref = mpl.colors.BoundaryNorm(bounds_ref, cmap.N)
first_date = datetime.datetime(1,1,1,0,0,0)
init_datetime = first_date+ datetime.timedelta(time[0])
#for t in range(1, len(time)):
print 'processing time %i ' %t
fct_datetime = first_date + datetime.timedelta(time[t])
fig_name = 'pcp_type_%s' %(fct_datetime.strftime("%Y%m%d_%H00"))
print fig_name
fig=plt.figure(figsize=(10, 10))
map1=m.contourf(x, y, rad_ref,len(bounds_ref),norm=norm_ref,cmap=cmap,levels=bounds_rain)
plt.title("RADAR | Init : %s Valid: %s" \
%(init_datetime.strftime("%HZ %Y-%m-%d"),fct_datetime.strftime("%HZ %Y-%m-%d")),loc='left')
plt.savefig("%s/%s.png" %(folder_path,fig_name), dpi=200,bbox_inches='tight' )
情节的顺序必须正确完成。 Filled-contour 是光栅类型,必须先绘制。然后,可以在上面绘制所有矢量数据。这是显示情节序列的代码片段:
# ...
# Raster data must be plotted first
map1 = m.contourf(x, y, rad_ref, len(bounds_ref), norm=norm_ref, cmap=cmap,
levels=bounds_rain)
# then, these line/vector data are plotted
m.drawstates()
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary()
# additionally, plot useful colorbar
plt.colorbar(shrink=0.55) # draw colorbar
# ...
结果图: