matplotlib 等高线图:有关“未找到等高线水平”的警告

kri*_*nab 5 python plot matplotlib contour

我正在研究有关绘制动画的博客文章中的matplotlib.一个示例,其中一个示例涉及等高线图——代码如下。但是,当我运行此代码时,我得到一个UserWarning

 UserWarning: No contour levels were found within the data range.
 warnings.warn("No contour levels were found")
Run Code Online (Sandbox Code Playgroud)

该图的代码如下。

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())
Run Code Online (Sandbox Code Playgroud)

情节仍然有效,但我不断收到用户警告。

查了matplotlib文档,发现levels参数还是正确的名字。所以不确定是什么导致了这个错误。

Imp*_*est 5

0 的正弦值为 0。因此,您得到 的一个完整切片G,即第一个G[:,:,0],其中全为零。零不是级别之一,但即使是,也没有定义绘制常量数组的轮廓(完整的表面应该是轮廓线吗?)

因此,matplotlib 会正确警告您此框架中没有要绘制的轮廓。