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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With