Matplotlib:如何再次显示情节? [英] Matplotlib: how to show plot again?

查看:54
本文介绍了Matplotlib:如何再次显示情节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我在 Ipython Notebook 中绘制了一个图,在几个单元格之后,我想再次渲染它,以便可以将其与其他图进行比较.

我该怎么做?

a = [1,2,3,4]b = [3,4,5,6]无花果= plt.plot(a,b,'-',color ='black')

这将显示绘图,但是当我运行 fig 时,没有绘图输出.

我发现: matplotlib再次显示图,但这看起来很复杂吗?

更新:这就是我最终的结果:

  def simple_plot(ax =无):如果ax为None:无花果,ax = plt.subplots()a = [1,2,3,4]b = [3,4,5,6]plt.plot(a,b,'-',color ='black')返回无花果fig = simple_plot() # 这将绘制图 # 这也将绘制

但是,如果我运行 simple_plot(),它将打印两次.

解决方案

你可以这样绘制:

a = [1,2,3,4]b = [3,4,5,6]f = plt.figure()f.add_subplot(111).plot(a, b,'-', color='black')

然后仅通过调用 f 重新渲染.

Let say I make a plot in Ipython Notebook, and after couples of cells, I want to render it again, so I can compare it with other plots.

How can I do it?

a = [1,2,3,4]
b = [3,4,5,6]
fig = plt.plot(a, b,'-', color='black')

This would display the plot, but when I run fig, there is no plot output.

I find this: matplotlib show figure again, but that seems pretty complex?

UPDATE: This is what I end up with:

def simple_plot(ax = None):
    if ax is None:
        fig, ax = plt.subplots()
    a = [1,2,3,4]
    b = [3,4,5,6]
    plt.plot(a, b,'-', color='black')
    return fig

fig = simple_plot() # This would plot
fig # this would also plot

However, if I run simple_plot(), it would print twice.

解决方案

You can plot like this:

a = [1,2,3,4]
b = [3,4,5,6]
f = plt.figure()
f.add_subplot(111).plot(a, b,'-', color='black')

Then render again just by calling f.

这篇关于Matplotlib:如何再次显示情节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆