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



我正在通过一篇关于用matplotlib.绘制动画的博客文章的示例 一个例子涉及等高线图——代码如下。但是,当我运行此代码时,我得到一个UserWarning

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

该图的代码如下。

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())

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

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

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

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

最新更新