Matplotlib contourplot fails when no contour exists

I use the cookbook example frin 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
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
# do the plotting and save the result
CS = plt.contour(xi, yi, zi)

Is there an elegant way to deal with this? Is this worthy of a ticket in matplotlib?


Why not just catch the exception, ie:

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

Need Your Help

Java: Basic OOP Question

java oop

I have written a program with both a Advanced Mode and a Beginner Mode to allow the user to get the hang of my program before it is actually used. The Advanced Mode is almost identical to the Begin...

Fullscreen Mode exiting

javascript html css html5

I have the following anchor tag: