ColorMap编码

时间:2018-11-09 22:23:13

标签: matplotlib-basemap 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,因为我正在尝试帮助一个朋友进行色盲编码。

1 个答案:

答案 0 :(得分:0)

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:颜色字符串或颜色序列,可选   关卡的颜色,即轮廓线和轮廓线f的区域。

     

序列以升序循环。如果序列短于级别数,则会重复。

     

作为一种捷径,可以使用单色字符串代替一个元素列表,即用'红色'代替['红色']用相同的颜色对所有色阶进行着色。此快捷方式仅适用于颜色字符串,不适用于其他指定颜色的方式。

     

默认情况下(值None),将使用cmap指定的颜色图。

如果您实际上是在尝试做一些涉及真正的ColorMap的事情,请参阅ListedColorMapLinearSegmentedColormap(甚至还有this SO answer)的文档,以获取有关转换的详细信息将颜色列表转换为适当的ColorMap