matplotlib - 如何让颜色条从暗到亮缩放?

标签 matplotlib plot colorbar

使用下面的代码,我绘制了两个矩形 block ,其颜色与算术值匹配。我已经包含了一个用于解释数据值的颜色条。如何使颜色条缩放为:深蓝色 --> 白色?

 import matplotlib.pyplot as plt
 import matplotlib.patches as patches
 from matplotlib import cm
 from matplotlib.colors import Normalize
 import numpy as np

 f,ax=plt.subplots()  
 blues=plt.get_cmap('Blues')

 norm = Normalize(vmin=0, vmax=2)
 sm = plt.cm.ScalarMappable(cmap=blues, norm=norm)
 sm._A = []
 ax.add_patch(patches.Rectangle((2,2),width=4.5, height=4, 
 facecolor=blues(norm(2)), alpha=1,label='a'))
 ax.add_patch(patches.Rectangle((2,-2),width=4.5, height=4, 
 facecolor=blues(norm(0.9)), alpha=1,label='b'))

 plt.colorbar(sm)
 plt.show()
 plt.xlim(xmin=-10,xmax=10)
 plt.ylim(ymin=-10,ymax=10)

 plt.show()

example output

最佳答案

对于任何内置颜色图,您可以将 _r 附加到其名称末尾以获取反转的颜色图。因此,请将 blues=plt.get_cmap('Blues') 替换为 blues=plt.get_cmap('Blues_r')

有关引用,请参见 this page 的底部:

This reference example shows all colormaps included with Matplotlib. Note that any colormap listed here can be reversed by appending "_r" (e.g., "pink_r").

关于matplotlib - 如何让颜色条从暗到亮缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574613/

相关文章:

python : How to create a 2D density map/heat map

python : Plot heatmap for large matrix

python - 属性错误 : 'Rectangle' object has no property 'norm_hist' - python

python - 为特定的 x 轴刻度设置不同的字体颜色

Python - 使用 LSTM 循环神经网络和 Keras 进行模式预测

python - matplotlib 中带箭头的线图

r - 在 R 中绘制家谱

r - 在 R 中绘制二元和连续值的多重逻辑回归

python - 使用 matplotlib 将色相渐变作为 y 轴上的颜色条

r - 图 同一图 Igraph 上子图社区的集合