在matplotlib pcolor中设置颜色

时间:2012-07-06 15:06:21

标签: python plot matplotlib

我想使用matplotlib在python中绘制类似第二个图像的内容: enter image description here

这背后的代码是here

#!/usr/bin/env python

from pylab import *

Z = rand(6,10)

subplot(2,1,1)
c = pcolor(Z)
title('default: no edges')

subplot(2,1,2)
c = pcolor(Z, edgecolors='k', linewidths=4)
title('thick edges')

show()

现在,我有一个布尔列表,我只想为每个True值绘制一个灰色矩形,为每个{{一个红色一个1}}值。

说我刚才有这个:

False

我应该将[0,1]中的值分配给True和False?

1 个答案:

答案 0 :(得分:5)

一种简单的方法是制作自定义色彩映射。在您的情况下,您可以只使用2个值创建colormap

from pylab import *
import matplotlib.colors

figure(figsize=(3,9))
Z = rand(6,10)

subplot(3,1,1)
c = pcolor(Z)
title('default: no edges')

subplot(3,1,2)
c = pcolor(Z, edgecolors='k', linewidths=4)
title('thick edges')
# use Z values greater than 0.5 as an example
Zbool = Z > 0.5

subplot(3,1,3)
cmap = matplotlib.colors.ListedColormap(['red','grey'])
c = pcolor(Zbool, edgecolors='k', linewidths=4, cmap=cmap)
title('thick boolean edges gray')

show()

Colormap example

相关问题