问题描述
当我绘制pcolormesh图时,请使用色图from matplotlib.cm
(例如"jet"
,"Set2"
等),我可以使用:
When I plot the pcolormesh plot use the colormap from matplotlib.cm
(like "jet"
, "Set2"
, etc), I can use:
cMap = plt.cm.get_cmap("jet",lut=6)
颜色栏显示如下:
但是如果我想从Basemap
包中调用颜色图(例如GMT_drywet
,GMT_no_green
等).我不能使用plt.cm,get_cmap
来获取这些颜色图并对其进行划分.
But if I want to call the colormap from the Basemap
package (like GMT_drywet
, GMT_no_green
, etc). I can't use plt.cm,get_cmap
to get these colormap and divide them.
mpl_toolkits.basemap.cm
是否具有与lut
类似的功能?
Does mpl_toolkits.basemap.cm
have a similiar function like lut
?
推荐答案
只要您制作的绘图具有离散的颜色值(例如contour
或contourf
),则colorbar
应该会自动生成带有不连续的步骤.这是一个基于来自basemap
文档的第一个示例的图:
As long as the plot you are making has discrete color values (e.g. contour
or contourf
), then colorbar
should automatically generate a colorbar with discrete steps. Here's a plot based on the first example from the basemap
documentation:
from mpl_toolkits.basemap import Basemap, cm
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 1)
ax.hold(True)
map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='aqua')
map.drawmapboundary(fill_color='aqua')
map.drawmeridians(np.arange(0,360,30))
map.drawparallels(np.arange(-90,90,30))
nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
lons = (delta*np.indices((nlats,nlons))[1,:,:])
wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)
x, y = map(lons*180./np.pi, lats*180./np.pi)
map.contourf(x,y,wave+mean,15, alpha=0.5, cmap=cm.GMT_drywet)
cb = map.colorbar()
plt.show()
这篇关于如何使用mpl_toolkits.basemap.cm中的色图创建离散色条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!