How to create a discrete color panel using a color map from mpl_toolkits.basemap.cm?

When I draw a graph pcolormesh, use the colormap from matplotlib.cm(eg "jet", "Set2"etc.), I can use:

 cMap = plt.cm.get_cmap("jet",lut=6)    

The color bar is as follows:

enter image description here

But if I want to call a colormap from the package Basemap(eg GMT_drywet, GMT_no_greenetc.). I can not use plt.cm,get_cmapto get this color scheme and separation.

Does it have a mpl_toolkits.basemap.cmsimilar function, for example lut?

+4
source share
2 answers

(, contour contourf), colorbar . , basemap:

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()

enter image description here

+2

@tacaswell comment , , _resample. pcolor/pcolormesh, , contourf. , , :

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import cm
plt.figure()
cmap = cm.GMT_drywet._resample(6)
pm = plt.pcolormesh(np.random.rand(10,8), cmap=cmap)
plt.colorbar(pm, orientation='horizontal')
plt.show()

enter image description here

+3

Source: https://habr.com/ru/post/1622577/


All Articles