【问题标题】:plt.show() does nothing when used for the second timeplt.show() 第二次使用时什么都不做
【发布时间】:2020-04-12 19:50:43
【问题描述】:

我刚开始在 Data Camp 上使用 python 学习数据科学,在使用 matplotlib.pyplot 中的函数时发现了一些东西

import matplotlib.pyplot as plt

year = [1500, 1600, 1700, 1800, 1900, 2000]
pop = [458, 580, 682, 1000, 1650, 6,127]

plt.plot(year, pop)

plt.show() # Here a window opens up and shows the figure for the first time

但是当我尝试再次显示它时它不会..

plt.show() # for the second time.. nothing happens

我必须重新输入show() 上方的行才能再次显示图形

这是正常现象还是问题?

注意:我使用的是 REPL

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    回答

    是的,这是 matplotlib 图形的正常预期行为。


    说明

    当您运行plt.plot(...) 时,一方面会创建实际情节的lines 实例:

    >>> print( plt.plot(year, pop) )
    [<matplotlib.lines.Line2D object at 0x000000000D8FDB00>]
    

    ...另一方面是Figure 实例,它被设置为“当前图形”,可通过plt.gcf()(“获取当前图形”的缩写)访问:

    >>> print( plt.gcf() )
    Figure(432x288)
    

    线条(以及您可能添加的其他绘图元素)都放置在当前图形中。当plt.show() 被调用时,当前图形会显示然后清空(!),这就是为什么第二次调用plt.show() 不会绘制任何东西。


    标准解决方法

    解决此问题的一种方法是显式保留当前的Figure 实例,然后直接使用fig.show() 显示它,如下所示:

    plt.plot(year, pop)
    fig = plt.gcf()  # Grabs the current figure
    
    plt.show()  # Shows plot
    plt.show()  # Does nothing
    
    fig.show()  # Shows plot again
    fig.show()  # Shows plot again...
    

    更常用的替代方法是在开始时显式初始化当前图形,在任何绘图命令之前。

    fig = plt.figure()   # Initializes current figure
    plt.plot(year, pop)  # Adds to current figure
    
    plt.show()  # Shows plot
    fig.show()  # Shows plot again
    

    这通常与图形的一些附加参数的规范相结合,例如:

    fig = plt.figure(figsize=(8,8))
    

    对于 Jupyter 笔记本用户

    fig.show() 方法在 Jupyter Notebooks 的上下文中可能不起作用,并且可能会产生以下警告并且不显示绘图:

    C:\redacted\path\lib\site-packages\matplotlib\figure.py:459: 用户警告:matplotlib 当前使用的是非 GUI 后端,所以 无法显示图

    幸运的是,只需在代码单元格的末尾写入fig(而不是fig.show())即可将图形推送到单元格的输出并显示它。如果需要在同一个代码单元格内多次显示,可以使用display函数达到同样的效果:

    fig = plt.figure()   # Initializes current figure
    plt.plot(year, pop)  # Adds to current figure
    
    plt.show()  # Shows plot
    plt.show()  # Does nothing
    
    from IPython.display import display
    display(fig)  # Shows plot again
    display(fig)  # Shows plot again...
    

    使用函数

    想要多次show 一个数字的一​​个原因是每次都要进行各种不同的修改。这可以使用上面讨论的fig 方法来完成,但对于更广泛的绘图定义,通常更容易将基本图形简单地包装在一个函数中并重复调用它。

    例子:

    def my_plot(year, pop):
        plt.plot(year, pop)
        plt.xlabel("year")
        plt.ylabel("population")
    
    my_plot(year, pop)
    plt.show()  # Shows plot
    
    my_plot(year, pop)
    plt.show()  # Shows plot again
    
    my_plot(year, pop)
    plt.title("demographics plot")
    plt.show()  # Shows plot again, this time with title
    

    【讨论】:

      【解决方案2】:

      使用命令:

      %matplotlib
      

      在 ipython3 中帮助我使用带有绘图的单独窗口

      【讨论】:

      • @DieterMeemken ,见上文
      【解决方案3】:

      启动 ipython3 并发出命令:

      import matplotlib.pyplot as plt
      %matplotlib #plot in separate window
      fig, ax = plt.subplots() #Appear empty Tcl window for image
      ax.plot([1, 2, 3, 4], [5, 6, 7, 8]) #Appear graph in window
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-09
        • 2020-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-08
        相关资源
        最近更新 更多