当没有轮廓存在时,Matplotlib contourplot失败

时间:2015-07-10 12:46:57

标签: python numpy matplotlib scipy

我使用cookbook示例frin http://wiki.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data制作等高线图。但是我的一些数据可能只包含零,在这种情况下,我得到一个ValueError:零大小的数组,以减少操作最小值,没有标识。

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
from numpy.random import uniform, seed


# make up some randomly distributed data
seed(1234)
npts = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = 0*x*np.exp(-x**2-y**2) #Here i multiply by zero
# define grid.
xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,100)
# grid the data.
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')

#zi[0][0]=0.00001 would make everything ok again
print(zi)
# do the plotting and save the result
CS = plt.contour(xi, yi, zi)
plt.show()

有一种优雅的方式可以解决这个问题吗? 这是否值得在matplotlib中出票?

1 个答案:

答案 0 :(得分:1)

为什么不抓住异常,即:

try:
    CS = plt.contour(xi, yi, zi)
    plt.show()
except ValueError:
    print("Can't plot this data")