Matplotlib contourplot fails when no contour exists
I use the cookbook example frin http://wiki.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data to make contourplots. However some of my data may just contain zeros, in which case I get an ValueError:zero-size array to reduction operation minimum which has no identity.
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.00001 would make everything ok again print(zi) # do the plotting and save the result CS = plt.contour(xi, yi, zi) plt.show()
Is there an elegant way to deal with this? Is this worthy of a ticket in matplotlib?
Why not just catch the exception, ie:
try: CS = plt.contour(xi, yi, zi) plt.show() except ValueError: print("Can't plot this data")