[英]How to plot the outlines of specific squares within a 2D grid using pcolormesh?
I have a (21 x 25) 2D array which contains two discrete values, "1" & "2". 我有一个(21 x 25)二维数组,其中包含两个离散值“ 1”和“ 2”。 The values are shown below:
值如下所示:
value = np.array(
[[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 1., 2., 2.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 1., 2., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
If I plot this using pcolor
, the resulting figure looks like this: 如果我使用
pcolor
绘制,则结果图如下所示:
I want to draw the outlines of the grid squares where value == 2
: 我想绘制其中
value == 2
的网格正方形的轮廓:
xx,yy = np.linspace(0,26,25),np.linspace(0,22,22)
xx,yy = np.meshgrid(xx,yy)
plt.pcolormesh(xx, yy, value, facecolor='none', edgecolor='b', alpha=0.8, zorder=2)
It seems like the outline isn't uniform (the lines have different shades), and I can't set the grid line colors by changing edgecolor
. 看起来轮廓不均匀(线条具有不同的阴影),并且我无法通过更改
edgecolor
来设置网格线颜色。
You can render the squares where value == 1
as transparent by creating an np.ma.masked_array
from value
: 通过从
value
创建一个np.ma.masked_array
,可以将value == 1
的正方形透明化:
c = np.ma.masked_array(value, value == 1.) # mask squares where value == 1
plt.pcolormesh(xx, yy, c, alpha=0.8, zorder=2, facecolor='none', edgecolors='k',
cmap='gray')
Setting the colormap to 'gray'
is a bit of a hack - unfortunately it seems that the default colormap overrides the edge color, even if you try to set it explicitly using edgecolor=
or edgecolors=
. 将颜色表设置为
'gray'
有点麻烦-不幸的是,即使您尝试使用edgecolor=
或edgecolors=
显式设置颜色,默认颜色表也会覆盖边缘颜色。 I'm not sure whether or not this should be considered a bug in matplotlib. 我不确定在matplotlib中是否应该将其视为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.