缺少值的pcolormesh? [英] pcolormesh with missing values?

查看:184
本文介绍了缺少值的pcolormesh?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个一维ndarray:x,y,z

I have 3 1-D ndarrays: x, y, z

和以下代码:

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as spinterp

## define data
npoints = 50
xreg = np.linspace(x.min(),x.max(),npoints)
yreg = np.linspace(y.min(),y.max(),npoints)
X,Y = np.meshgrid(xreg,yreg)
Z = spinterp.griddata(np.vstack((x,y)).T,z,(X,Y),
                      method='linear').reshape(X.shape)

## plot
plt.close()
ax = plt.axes()
col = ax.pcolormesh(X,Y,Z.T)
plt.draw()

我的图空白了,我怀疑是因为方法='linear'插值是用nans出来的.我试过转换为带掩码的数组,但无济于事-情节仍然是空白.你能告诉我我做错了什么吗?谢谢.

My plot comes out blank, and I suspect it is because the method='linear' interpolation comes out with nans. I've tried converting to a masked array, but to no avail - plot is still blank. Can you tell me what I am doing wrong? Thanks.

推荐答案

知道了.这似乎是回旋的,但这是解决方案:

Got it. This seems round-about, but this was the solution:

import numpy.ma as ma

Zm = ma.masked_where(np.isnan(Z),Z)
plt.pcolormesh(X,Y,Zm.T)

如果Z矩阵包含nan,则它必须是pcolormesh的掩码数组,必须使用ma.masked_where

If the Z matrix contains nan's, it has to be a masked array for pcolormesh, which has to be created with ma.masked_where, or, alternatively,

Zm = ma.array(Z,mask=np.isnan(Z))

这篇关于缺少值的pcolormesh?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆