【问题标题】:matplotlib contour plot: warning about `no contour levels were found`matplotlib 等高线图:关于“未找到等高线级别”的警告
【发布时间】:2018-07-02 15:37:15
【问题描述】:

我在博客post 上研究了一个关于使用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参数仍然是正确的名称。所以不确定是什么导致了这个错误。

【问题讨论】:

    标签: python matplotlib plot contour


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2021-06-14
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多