在Matplotlib中自定义颜色-热图 [英] Customizing colors in matplotlib - heatmap
本文介绍了在Matplotlib中自定义颜色-热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在热图中指定颜色。
在此示例中,数据唯一地是4个值之一 {0,1,2,3}
How can I specify colors in heatmap.
In this example, the data are uniquely one of 4 values {0,1,2,3}
Index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Cols = ['A', 'B', 'C', 'D']
data= [[ 0, 3, 1, 1],[ 0, 1, 1, 1],[ 0, 1, 2, 1],[ 0, 2, 1, 2],[ 0, 1, 1, 1]]
print data
df = pd.DataFrame(data, index=Index, columns=Cols)
heatmap = plt.pcolor(np.array(data))
plt.colorbar(heatmap)
plt.show()
我该如何指定这些颜色来表示
colors = {0:'green',1:'red',2:'black',3:'yellow '}
How can I specifiy those colors in a way to represent colors= {0:'green',1:'red',2:'black',3:'yellow'}
推荐答案
创建自定义颜色图并将刻度设置为整数
Create custom colormap and set ticks to your integers
from matplotlib import colors
cmap = colors.ListedColormap(['green','red','black','yellow'])
bounds=[-0.5, 0.5, 1.5, 2.5, 3.5]
norm = colors.BoundaryNorm(bounds, cmap.N)
heatmap = plt.pcolor(np.array(data), cmap=cmap, norm=norm)
plt.colorbar(heatmap, ticks=[0, 1, 2, 3])
这是您想要的吗?
请注意,您的数据
显示为倒置。
Is this what you want?
Notice, that your data
are displayed "upside down".
这篇关于在Matplotlib中自定义颜色-热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文