带隐藏的无效值的pcolormesh [英] pcolormesh with masked invalid values
问题描述
我正在尝试将一维数组绘制为pcolormesh(因此颜色沿x轴变化,但每个x的y轴均不变).但是我的数据有一些错误的值,所以我使用了一个蒙版数组和一个自定义颜色表,其中蒙版值设置为蓝色:
I'm trying to plot a one-dimensional array as a pcolormesh (so the color varies along the x-axis, but is constant in the y-axis for each x). But my data has some bad values, so I'm using a masked array and a customized colormap with masked values set to blue:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import copy
a = np.array([3, 5, 10, np.inf, 5, 8])
a = np.ma.masked_where(np.isinf(a), a)
imdata = np.vstack((a, a))
myhot = copy.copy(cm.hot)
myhot.set_bad('b', 1)
fig, ax = plt.subplots()
im = ax.pcolormesh(imdata, cmap=myhot)
plt.colorbar(im)
plt.show()
如果我没有np.inf
值,它可以很好地工作,但是如果我没有,则只能得到一个空白图.我似乎误解了set_bad
的工作方式,因为我得到了另外的警告:
It works fine if I don't have the np.inf
value, but I just get a blank plot if I do. I seem to have misunderstood something about the way set_bad
works because I get an additional warning:
RuntimeWarning: invalid value encountered in true_divide
resdat /= (vmax - vmin)
我应该怎么做才能获得想要的效果?
What should I be doing to get the effect I want?
推荐答案
您需要掩盖imdata
,不一定是a
:
You need to mask imdata
, not necessarily a
:
import numpy as np
import matplotlib.pyplot as plt
a = np.array([3, 5, 10, np.inf, 5, 8])
imdata = np.ma.masked_invalid(np.atleast_2d(a))
cmap = plt.cm.hot
cmap.set_bad('b', 1)
fig, ax = plt.subplots()
im = ax.pcolormesh(imdata, cmap=cmap)
plt.colorbar(im)
plt.show()
如果您在互动会话中查看imdata
,您会看到
If you look at imdata
in an interactive session, you'll see
In [185]: imdata
Out[185]:
masked_array(data =
[[ 3. 5. 10. inf 5. 8.]
[ 3. 5. 10. inf 5. 8.]],
mask =
False,
fill_value = 1e+20)
以上,mask=False
表示未屏蔽任何内容.如果用np.ma.masked_invalid
包装,则:
Above, mask=False
means that nothing is masked. If you wrap that with np.ma.masked_invalid
then:
In [186]: np.ma.masked_invalid(imdata)
Out[186]:
masked_array(data =
[[3.0 5.0 10.0 -- 5.0 8.0]
[3.0 5.0 10.0 -- 5.0 8.0]],
mask =
[[False False False True False False]
[False False False True False False]],
fill_value = 1e+20)
遮罩a
的问题是np.vstack
不遵守遮罩.
或者,您可以使用np.ma.vstack
.一般来说,只有
np.ma
命名空间中的函数尊重掩码.
The problem with masking a
is that np.vstack
does not respect the mask.
Alternatively, you could have used np.ma.vstack
. Generally speaking, only
functions in the np.ma
namespace respect the mask.
但是,您实际上并不需要在这里使用vstack
. np.atleast_2d
可以.
vstack
创建形状为(2, N)
的数组,而np.atleast_2d
创建形状为(1, N)
的数组.
However, you don't actually need to use vstack
here; np.atleast_2d
will do.
vstack
creates an array of shape (2, N)
, while np.atleast_2d
creates an array of shape (1, N)
.
另一种替代方法是使用set_over
代替set_bad
.这将允许
您可以避免完全使用带掩码的数组:
Another alternative is to use set_over
instead of set_bad
. This would allow
you to avoid needing a masked array altogether:
import numpy as np
import matplotlib.pyplot as plt
a = np.array([3, 5, 10, np.inf, 5, 8])
imdata = np.atleast_2d(a)
cmap = plt.cm.hot
cmap.set_over('b')
cmap.set_under('g')
fig, ax = plt.subplots()
b = a[np.isfinite(a)]
im = ax.pcolormesh(imdata, cmap=cmap, vmin=b.min(), vmax=b.max())
plt.colorbar(im, extend='both')
plt.show()
extend='both'
与set_over
和set_under
结合使用会在颜色栏上给您一些彩色的箭头,指示用于颜色栏范围之外的值的颜色.
The extend='both'
in conjunction with set_over
and set_under
give you little colored arrows on the colorbar which indicate the color used for values beyond the colorbar's range.
这篇关于带隐藏的无效值的pcolormesh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!