Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib contour plot: warning about `no contour levels were found`

I was working through an example on a blog post about plotting animations with matplotlib. One example involved a contour plot--the code is below. However when I run this code, I get a UserWarning

 UserWarning: No contour levels were found within the data range.
 warnings.warn("No contour levels were found")

The code for the plot is below.

x = np.linspace(-3,3,91)
t = np.linspace(0,25,30)
y = np.linspace(-3,3,91)
X3, Y3, T3 = np.meshgrid(x,y, t)

sinT3 = np.sin(2*np.pi*T3/T3.max(axis=2)[...,np.newaxis])
G = (X3**2 + Y3**2)*sinT3
contour_opts = {'levels': np.linspace(-9, 9, 10),
                'cmap':'RdBu', 'linewidths': 2}
cax = ax.contour(x, y, G[..., 0], **contour_opts)

def animate(i):
    ax.collections = []
    ax.contour(x, y, G[..., i], **contour_opts)
anim = FuncAnimation(fig, animate, interval=100, frames = len(t)-1)

HTML(anim.to_html5_video())

The plot still works, but I keep getting that user warning.

I checked the matplotlib documentation and found that the levels parameter is still the correct name. So not sure what is causing this error.

like image 643
krishnab Avatar asked Oct 17 '22 23:10

krishnab


1 Answers

The sinus of 0 is 0. Hence you get one complete slice of G, namely the first one G[:,:,0], which has all zeros in it. Zero is not one of the levels, but even if it was, drawing a contour of a constant array is not defined (should the complete surface be the line of contour?)

Therefore matplotlib correctly warns you that there is no contour to be drawn in this frame.

like image 52
ImportanceOfBeingErnest Avatar answered Oct 21 '22 01:10

ImportanceOfBeingErnest