plt.show()第二次使用时不执行任何操作 [英] plt.show() does nothing when used for the second time

查看:119
本文介绍了plt.show()第二次使用时不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在Data Camp上使用python学习数据科学,并且在使用matplotlib.pyplot中的功能时发现了一些问题

I am just starting to learn data science using python on Data Camp and I noticed something while using the functions in 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

但是当我尝试再次显示它时,它没有..

but when I try to show it again it doesn't..

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

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

And I have to retype the line above the show() to be able to show a figure again

这是正常现象还是问题?

Is this the normal thing or a problem?

注意:我正在使用REPL

Note: I am using the REPL

推荐答案

答案

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

Answer

Yes, this is normal expected behavior for matplotlib figures.

当你运行 plt.plot(...) 时,你一方面创建了实际绘图的 lines 实例:

When you run plt.plot(...) you create on the one hand the lines instance of the actual plot:

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

...,另一方面,是一个 Figure 实例,该实例被设置为当前图形",可通过 plt.gcf()(获取当前数字"):

...and on the other hand a Figure instance, which is set as the 'current figure' and accessible through plt.gcf() (short for "get current figure"):

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

线(以及您可能添加的其他绘图元素)都放置在当前图形中.调用 plt.show()时,先显示当前图形,然后将其清空(!),这就是第二次调用 plt.show()的原因 不绘制任何内容.

The lines (as well as other plot elements you might add) are all placed in the current figure. When plt.show() is called, the current figure is displayed and then emptied (!), which is why a second call of plt.show() doesn't plot anything.

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

One way of solving this is to explicitly keep hold of the current Figure instance and then show it directly with fig.show(), like this:

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...

一个更常用的替代方法是在开始时明确初始化当前图形,在任何绘图命令之前.

A more commonly used alternative is to initialize the current figure explicitly in the beginning, prior to any plotting commands.

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

plt.show()  # Shows plot
fig.show()  # Shows plot again

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

This is often combined with the specification of some additional parameters for the figure, for example:

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

<小时>

对于Jupyter Notebook用户

fig.show() 方法可能不适用于 Jupyter Notebooks 的上下文,而是可能产生以下警告而不显示绘图:


For Jupyter Notebook Users

The fig.show() approach may not work in the context of Jupyter Notebooks and may instead produce the following warning and not show the plot:

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

C:\redacted\path\lib\site-packages\matplotlib\figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure

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

Fortunately, simply writing fig at the end of a code cell (instead of fig.show()) will push the figure to the cell's output and display it anyway. If you need to display it multiple times from within the same code cell, you can achieve the same effect using the display function:

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...

<小时>

利用函数

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


Making Use of Functions

One reason for wanting to show a figure multiple times is to make a variety of different modifications each time. This can be done using the fig approach discussed above but for more extensive plot definitions it is often easier to simply wrap the base figure in a function and call it repeatedly.

示例:

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

这篇关于plt.show()第二次使用时不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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