【问题标题】:ColorMap Coding颜色映射编码
【发布时间】:2019-04-13 12:20:07
【问题描述】:

我在使用专门的色阶和底图进行一些编码时遇到问题

    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 问题,因为我正在尝试帮助朋友编写色盲编码。

【问题讨论】:

    标签: matplotlib-basemap colormap


    【解决方案1】:

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

    序列按升序循环。如果序列短于层数,则重复。

    作为一种快捷方式,可以使用单一颜色字符串来代替单元素列表,即“红色”而不是 [“红色”] 以用相同的颜色为所有级别着色。此快捷方式仅适用于颜色字符串,不适用于其他指定颜色的方式。

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

    如果您实际上是在尝试做一些涉及真正ColorMap 的更有趣的事情,请参阅ListedColorMapLinearSegmentedColormap(可能还有this SO answer)的文档,了解有关将颜色列表转换为正确@ 的详细信息987654334@.

    【讨论】:

    • 等我回来试试
    • 它有效,但它给了我一个尴尬的色标。
    猜你喜欢
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2021-05-10
    • 2017-12-23
    • 2021-09-22
    相关资源
    最近更新 更多