色图编码
ColorMap Coding
我在使用专用色标和底图进行编码时遇到问题
levels=[35,45,55,65,75,85,95,105,115]
CB_color_cycle = ['#377eb8', '#ff7f00', '#4daf4a','#f781bf', '#a65628',
'#984ea3','#999999', '#e41a1c', '#dede00']
fig=plt.figure(501)
m=Basemap(projection='merc',llcrnrlat=27,urcrnrlat=47,llcrnrlon=234,urcrnrlon=285,resolution='h')
px,py=N.meshgrid(lon+360,lat,sparse="True")
X,Y=m(px,py)
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.fillcontinents(color='gray',alpha=0.1,lake_color='aqua')
m.drawcounties()
#plotting contours
cs=m.contourf(lon+360,lat,tempsfc,levels=levels,cmap=CB_color_cycle,latlon=True,extend='both')
cs2=m.contour(lon+360,lat,tempsfc,levels=levels,latlon=True,colors='k',linestyles='solid')
cbar=plt.colorbar(cs)
cbar.add_lines(cs2)
cbar.ax.set_ylabel('Temp F')
plt.suptitle("SFC Temperature June 26,2017 at 19Z")
plt.show()
但是我遇到了这个错误,尽管我有使用 Python 的经验,但我以前从未处理过这个问题,因为我正在尝试使用特殊的色标。
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.py:3608: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
b = ax.ishold()
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.py:3675: MatplotlibDeprecationWarning: axes.hold is deprecated.
See the API Changes document (http://matplotlib.org/api/api_changes.html)
for more details.
ax.hold(b)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\Users\stratus\Documents\TestforMattsPaper.py in <module>()
36 #plotting contours
37
---> 38 cs=m.contourf(lon+360,lat,tempsfc,levels=levels,cmap=CB_color_cycle,latlon=True,extend='both')
39 cs2=m.contour(lon+360,lat,tempsfc,levels=levels,latlon=True,colors='k',linestyles='solid')
40
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in with_transform(self, x, y, data, *args, **kwargs)
519 # convert lat/lon coords to map projection coords.
520 x, y = self(x,y)
--> 521 return plotfunc(self,x,y,data,*args,**kwargs)
522 return with_transform
523
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in contourf(self, x, y, data, *args, **kwargs)
3671 mask = np.logical_or(ma.getmaskarray(data),xymask)
3672 data = ma.masked_array(data,mask=mask)
-> 3673 CS = ax.contourf(x,y,data,*args,**kwargs)
3674 except:
3675 ax.hold(b)
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\__init__.pyc in inner(ax, *args, **kwargs)
1890 warnings.warn(msg % (label_namer, func.__name__),
1891 RuntimeWarning, stacklevel=2)
-> 1892 return func(ax, *args, **kwargs)
1893 pre_doc = inner.__doc__
1894 if pre_doc is None:
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_axes.pyc in contourf(self, *args, **kwargs)
5827 self.cla()
5828 kwargs['filled'] = True
-> 5829 contours = mcontour.QuadContourSet(self, *args, **kwargs)
5830 self.autoscale_view()
5831 return contours
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in __init__(self, ax, *args, **kwargs)
862 self._transform = kwargs.get('transform', None)
863
--> 864 self._process_args(*args, **kwargs)
865 self._process_levels()
866
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in _process_args(self, *args, **kwargs)
1427 self._corner_mask = mpl.rcParams['contour.corner_mask']
1428
-> 1429 x, y, z = self._contour_args(args, kwargs)
1430
1431 _mask = ma.getmask(z)
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in _contour_args(self, args, kwargs)
1518 warnings.warn('Log scale: values of z <= 0 have been masked')
1519 self.zmin = float(z.min())
-> 1520 self._contour_level_args(z, args)
1521 return (x, y, z)
1522
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in _contour_level_args(self, z, args)
1184 warnings.warn("Contour levels are not increasing")
1185 else:
-> 1186 raise ValueError("Contour levels must be increasing")
1187
1188 def _process_levels(self):
ValueError: Contour levels must be increasing
缩进不是问题,而是 CB_color_cycle 问题,因为我正试图帮助一位色盲朋友编码。
CB_color_cycle
只是一个颜色列表,所以使用 colors
关键字而不是 cmap
:
cs = m.contourf(lon+360, lat, tempsfc, levels=levels, colors=CB_color_cycle, latlon=True, extend='both')
这是关于 contourf
docs 中的 colors
关键字的说明:
colors : color string or sequence of colors, optional
The colors of the levels, i.e. the lines for contour and the areas for contourf.
The sequence is cycled for the levels in ascending order. If the sequence is shorter than the number of levels, it's repeated.
As a shortcut, single color strings may be used in place of one-element lists, i.e. 'red' instead of ['red'] to color all levels with the same color. This shortcut does only work for color strings, not for other ways of specifying colors.
By default (value None), the colormap specified by cmap will be used.
如果您实际上想做一些涉及真实 ColorMap
的更有趣的事情,请参阅 ListedColorMap
or LinearSegmentedColormap
(and maybe also ) 的文档以了解有关将颜色列表转换为正确 ColorMap
的详细信息。
我在使用专用色标和底图进行编码时遇到问题
levels=[35,45,55,65,75,85,95,105,115]
CB_color_cycle = ['#377eb8', '#ff7f00', '#4daf4a','#f781bf', '#a65628',
'#984ea3','#999999', '#e41a1c', '#dede00']
fig=plt.figure(501)
m=Basemap(projection='merc',llcrnrlat=27,urcrnrlat=47,llcrnrlon=234,urcrnrlon=285,resolution='h')
px,py=N.meshgrid(lon+360,lat,sparse="True")
X,Y=m(px,py)
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.fillcontinents(color='gray',alpha=0.1,lake_color='aqua')
m.drawcounties()
#plotting contours
cs=m.contourf(lon+360,lat,tempsfc,levels=levels,cmap=CB_color_cycle,latlon=True,extend='both')
cs2=m.contour(lon+360,lat,tempsfc,levels=levels,latlon=True,colors='k',linestyles='solid')
cbar=plt.colorbar(cs)
cbar.add_lines(cs2)
cbar.ax.set_ylabel('Temp F')
plt.suptitle("SFC Temperature June 26,2017 at 19Z")
plt.show()
但是我遇到了这个错误,尽管我有使用 Python 的经验,但我以前从未处理过这个问题,因为我正在尝试使用特殊的色标。
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.py:3608: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
b = ax.ishold()
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.py:3675: MatplotlibDeprecationWarning: axes.hold is deprecated.
See the API Changes document (http://matplotlib.org/api/api_changes.html)
for more details.
ax.hold(b)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\Users\stratus\Documents\TestforMattsPaper.py in <module>()
36 #plotting contours
37
---> 38 cs=m.contourf(lon+360,lat,tempsfc,levels=levels,cmap=CB_color_cycle,latlon=True,extend='both')
39 cs2=m.contour(lon+360,lat,tempsfc,levels=levels,latlon=True,colors='k',linestyles='solid')
40
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in with_transform(self, x, y, data, *args, **kwargs)
519 # convert lat/lon coords to map projection coords.
520 x, y = self(x,y)
--> 521 return plotfunc(self,x,y,data,*args,**kwargs)
522 return with_transform
523
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in contourf(self, x, y, data, *args, **kwargs)
3671 mask = np.logical_or(ma.getmaskarray(data),xymask)
3672 data = ma.masked_array(data,mask=mask)
-> 3673 CS = ax.contourf(x,y,data,*args,**kwargs)
3674 except:
3675 ax.hold(b)
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\__init__.pyc in inner(ax, *args, **kwargs)
1890 warnings.warn(msg % (label_namer, func.__name__),
1891 RuntimeWarning, stacklevel=2)
-> 1892 return func(ax, *args, **kwargs)
1893 pre_doc = inner.__doc__
1894 if pre_doc is None:
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_axes.pyc in contourf(self, *args, **kwargs)
5827 self.cla()
5828 kwargs['filled'] = True
-> 5829 contours = mcontour.QuadContourSet(self, *args, **kwargs)
5830 self.autoscale_view()
5831 return contours
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in __init__(self, ax, *args, **kwargs)
862 self._transform = kwargs.get('transform', None)
863
--> 864 self._process_args(*args, **kwargs)
865 self._process_levels()
866
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in _process_args(self, *args, **kwargs)
1427 self._corner_mask = mpl.rcParams['contour.corner_mask']
1428
-> 1429 x, y, z = self._contour_args(args, kwargs)
1430
1431 _mask = ma.getmask(z)
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in _contour_args(self, args, kwargs)
1518 warnings.warn('Log scale: values of z <= 0 have been masked')
1519 self.zmin = float(z.min())
-> 1520 self._contour_level_args(z, args)
1521 return (x, y, z)
1522
C:\Users\stratus\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\contour.pyc in _contour_level_args(self, z, args)
1184 warnings.warn("Contour levels are not increasing")
1185 else:
-> 1186 raise ValueError("Contour levels must be increasing")
1187
1188 def _process_levels(self):
ValueError: Contour levels must be increasing
缩进不是问题,而是 CB_color_cycle 问题,因为我正试图帮助一位色盲朋友编码。
CB_color_cycle
只是一个颜色列表,所以使用 colors
关键字而不是 cmap
:
cs = m.contourf(lon+360, lat, tempsfc, levels=levels, colors=CB_color_cycle, latlon=True, extend='both')
这是关于 contourf
docs 中的 colors
关键字的说明:
colors : color string or sequence of colors, optional The colors of the levels, i.e. the lines for contour and the areas for contourf.
The sequence is cycled for the levels in ascending order. If the sequence is shorter than the number of levels, it's repeated.
As a shortcut, single color strings may be used in place of one-element lists, i.e. 'red' instead of ['red'] to color all levels with the same color. This shortcut does only work for color strings, not for other ways of specifying colors.
By default (value None), the colormap specified by cmap will be used.
如果您实际上想做一些涉及真实 ColorMap
的更有趣的事情,请参阅 ListedColorMap
or LinearSegmentedColormap
(and maybe also ColorMap
的详细信息。