在带有离散色条的pcolormesh图中使用扩展 [英] Use of extend in a pcolormesh plot with discrete colorbar
问题描述
我正在尝试创建带有离散色条的pcolormesh图.输出应满足以下条件:
I'm trying to create a pcolormesh plot with a discrete colorbar. The output should fulfil these criteria:
- 第一级应该是白色
- 应该在某种程度上切断数据
- 截止点上方的数据应具有单独的颜色(即颜色图的最后一种颜色)
我快到了,但是'extend'关键字的行为不符合我的期望("max-arrow"中的颜色与上一级的颜色相同-参见示例).如何将"vmax"以上的值设置为单独的颜色(即我使用的任何颜色图的最后一种颜色)
I am almost there but the 'extend' keyword does not behave the way I would expect it to (the colour in the "max-arrow" is the same as for the last level - see example). How do I set values above 'vmax' to a separate colour (i.e., the last colour of whatever colormap I use)
import numpy as np
import xarray as xr
import matplotlib as mpl
import matplotlib.pyplot as plt
ds = xr.Dataset(
coords={'lon': np.arange(-180, 180, 10),
'lat': np.arange(-85, 90, 10)},
data_vars={'data': (('lat', 'lon'), np.random.rand(18, 36))})
cmap = plt.cm.get_cmap('Reds')
cmap.set_under('w')
# cmap.set_over() # do something here?
levels = np.arange(0, .7, .1)
ds.data.plot.pcolormesh(
cmap=cmap,
vmin=levels[1],
# vmax=levels[-1],
extend='max',
norm = mpl.colors.BoundaryNorm(levels, ncolors=cmap.N, clip=False)
)
我正在使用xarray,但是plt.pcolormesh的行为是相同的:
I'm using xarray but the behaviour is the same for plt.pcolormesh:
p = plt.pcolormesh(
np.arange(-180, 180, 10),
np.arange(-85, 90, 10),
np.random.rand(18, 36),
cmap=cmap,
vmin=levels[1],
# vmax=levels[-1],
norm = mpl.colors.BoundaryNorm(levels, ncolors=cmap.N, clip=False)
)
plt.colorbar(p, extend='max')
推荐答案
实际上,如果设置 cmap.set_over("blue")
,您会看到蓝色代表值的颜色超过最大值值.
Indeed, if you set cmap.set_over("blue")
you would see blue as the color of the values exceeding the maximum value.
但是,如果要将颜色图的最后一种颜色用作 set_over
的颜色,则需要制作一个颜色图,该颜色从第二个颜色停止.为此,可以使用以下原理.如果我们针对颜色表中的6种不同颜色加上用于超调值的颜色,则从该颜色表中获取7种颜色,将第一种颜色替换为白色,然后将前6种颜色用作边界间隔的颜色.然后将最后的颜色用作超调值的颜色.
However, if you want to use the last color of the colormap as that color for set_over
you need to make a colormap, which stops at the second last color. To that end, the following rationale may be used. If we aim at 6 different colors from a colormap plus the color for overshooting values, we take 7 colors from that colormap, replace the first with white color and use the first 6 colors as the colors for the boundary interval. The last colors is then used as the color for overshooting values.
import numpy as np; np.random.seed(1)
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors
lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10))
data = np.sort(np.random.rand(18, 36),axis=1)
# create 7 boundaries between 0 and 0.6, to have 6 intervals
boundaries = np.arange(0, .7, .1)
# create list of 7(!) colors from colormap
cmap_reds = plt.cm.get_cmap('Reds',len(boundaries))
colors = list(cmap_reds(np.arange(len(boundaries))))
#replace first color with white
colors[0] = "white"
cmap = matplotlib.colors.ListedColormap(colors[:-1], "")
# set over-color to last color of list
cmap.set_over(colors[-1])
cm = plt.pcolormesh(lon,lat,data,
cmap=cmap,
norm = mpl.colors.BoundaryNorm(boundaries, ncolors=len(boundaries)-1, clip=False)
)
plt.colorbar(cm, extend="max")
plt.show()
这篇关于在带有离散色条的pcolormesh图中使用扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!