pyplot.contourf 如何从颜色图中选择颜色? [英] How does pyplot.contourf choose colors from a colormap?
问题描述
我一直在使用 from_levels_and_colors
函数,这样我就可以在pcolormesh图上有一个扩展的颜色栏,类似于contourf.这是我的轮廓轮廓图示例:
I have been playing with the from_levels_and_colors
function, so that I can have an extended colorbar on a pcolormesh plot, similar to contourf. Here is my example contourf plot:
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(12)[:,np.newaxis] * np.ones(8)
levels = np.arange(1.5, 10, 2)
plt.contourf(a, cmap='RdYlBu', levels=levels, extend='both')
plt.colorbar()
要生成类似的 pcolormesh 图,我需要提供一系列颜色,所以我有:
To produce an analogous pcolormesh plot I need to supply a sequence of colors, so I have:
from matplotlib.colors import from_levels_and_colors
n_colors = len(levels) + 1
cmap = plt.get_cmap('RdYlBu', n_colors)
colors = cmap(range(cmap.N))
cmap, norm = from_levels_and_colors(levels, colors, extend='both')
plt.pcolormesh(a, cmap=cmap, norm=norm)
plt.colorbar()
pcolormesh中的中间四种颜色比轮廓f中的浅.我如何选择它们以使其匹配?
The middle four colors in the pcolormesh are lighter than in the contourf. How might I choose them so they match?
推荐答案
问题是 contourf
绘图的颜色取自各自区间的中间.要为 pcolor
绘图复制相同的行为,您需要从颜色图中选择颜色而不仅仅是等距范围 (colors = cmap(range(cmap.N))
),但作为地图的两个端点以及各个级别边界之间的均值.
The problem is that the colors for a contourf
plot are taken from the middle of the respecive interval. To replicate the same behaviour for a pcolor
plot, you need to select the colors not simply as equally spaced range from the colormap (colors = cmap(range(cmap.N))
), but as the two endpoints of the map and the respective means between the level boundaries.
cnorm = plt.Normalize(vmin=levels[0],vmax=levels[-1])
clevels = [levels[0]] + list(0.5*(levels[1:]+levels[:-1])) + [levels[-1]]
colors=plt.cm.RdYlBu(cnorm(clevels))
完整代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
a = np.arange(12)[:,np.newaxis] * np.ones(8)
levels = np.arange(1.5, 10, 2)
fig, (ax,ax2) = plt.subplots(ncols=2)
ax.set_title("contourf")
cf = ax.contourf(a, levels=levels, cmap='RdYlBu', extend='both') #,
fig.colorbar(cf, ax=ax)
##### pcolormesh
cnorm = plt.Normalize(vmin=levels[0],vmax=levels[-1])
clevels = [levels[0]] + list(0.5*(levels[1:]+levels[:-1])) + [levels[-1]]
colors=plt.cm.RdYlBu(cnorm(clevels))
cmap, norm = matplotlib.colors.from_levels_and_colors(levels, colors, extend='both')
cf = ax2.pcolormesh(a, cmap=cmap, norm=norm)
ax2.set_title("pcolormesh")
fig.colorbar(cf,ax=ax2)
plt.tight_layout()
plt.show()
为了更好地理解解决方案,您可能需要将 cmap, norm = matplotlib.colors.from_levels_and_colors(levels, colors, extend='both')
行替换为
To better understand the solution, you may want to replace the line cmap, norm = matplotlib.colors.from_levels_and_colors(levels, colors, extend='both')
by
norm=matplotlib.colors.BoundaryNorm(levels, ncolors=len(levels)-1)
cmap = matplotlib.colors.ListedColormap(colors[1:-1], N=len(levels)-1)
cmap.set_under(colors[0])
cmap.set_over(colors[-1])
cmap.colorbar_extend = "both"
这可能会更清楚,最终使用的颜色和颜色图的来源.
This may make it clearer, where the colors and the colormap eventually used originate from.
这篇关于pyplot.contourf 如何从颜色图中选择颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!