4

我想绘制与某些值关联的矩形(X_i)。每个矩形都将根据其值 value 具有填充颜色X_i

是否可以使用一个colorbar并且还知道哪种颜色与一个值相关联?

4

1 回答 1

9
from matplotlib.pylab import *
import matplotlib.cm as cm
import matplotlib.patches as patches

min_val = 0
max_val = 50

my_cmap = cm.get_cmap('jet') # or any other one
norm = matplotlib.colors.Normalize(min_val, max_val) # the color maps work for [0, 1]

x_i = 15
color_i = my_cmap(norm(x_i)) # returns an rgba value

rect = patches.Rectangle((.5, .5), .25, .25, color=color_i) # make your rectangle

cmmapable = cm.ScalarMappable(norm, my_cmap)
cmmapable.set_array(range(min_val, max_val))

figure()
ax = gca()
ax.add_patch(rect)
colorbar(cmmapable)
show()

还会在图表上为您提供一个颜色条。

于 2013-03-05T23:45:55.810 回答