简体   繁体   English

plt.plot(x,y)和plt.show()如何按照他们的方式工作?

[英]How do plt.plot(x,y) and plt.show() work the way they do?

I wanted to know the basic backbone process going on between plt.plot(x,y) and plt.show() commands of matplotlib.pyplot . 我想知道matplotlib.pyplot的 plt.plot(x,y)plt.show()命令之间plt.plot(x,y)的基本主干过程。

To elaborate it a bit, the piece of code: 详细说明一下,这段代码:

plt.plot(x , y)
plt.show()

show the desired graph (no problem with that). 显示所需的图形(没有问题)。

Also, the code: 另外,代码:

plt.plot(x , y)
plt.plot(p , q)
plt.show()

works fine as well. 工作得很好。 It shows the two plots created by the lists x & y and p & q. 它显示了由列表x和y以及p&q创建的两个图。

Now here is something that I found very interesting when coding dynamically in ipython. 现在,在ipython中动态编码时,我发现这里非常有趣。

In [73]: plt.plot(x , y)
#normal plotting function.
In [78]: plt.show()
#shows a graph as intended.
In [79]: plt.show()
#shows nothing.

Now, no matter how many times I call plt.show() (after I've called it once) it doesn't display the graph at all. 现在,无论我多少次调用plt.show() (在我调用它之后),它根本不显示图形。 Why is so? 为什么会这样? .

PS: To my understanding maybe there is an object being created and deleted withing this process. PS:据我所知,可能有一个对象正在创建和删除这个过程。 But neither I'm sure nor convinced. 但我既不确定也不相信。

Thanks in advance. 提前致谢。

Pyplot uses or is a so called "statemachine". Pyplot使用或是所谓的“statemachine”。 It stores a number of figures and references to the current axes and figure. 它存储了许多数字和当前轴和图的参考。 Once show is called, all figures are shown and once show returns, they are removed from the statemachine. 一旦调show ,所有数字都会显示,一旦show返回,它们就会从状态机中删除。

On a subsequent call to show there are no figures to show anymore, hence not output is shown. 在随后的调用中show没有数字可以显示,因此不显示输出。

Therefore there is some (maybe unwritten or implicit) assumption that show is called exactly once in a script. 因此,有一些(可能是不成文的或隐式的)假设show在脚本中只调用一次。

It may be worth noting that although figures are removed from the statemachine they remain in memory until they are closed. 值得注意的是,尽管从状态机中删除了数字,但它们仍会保留在内存中,直到它们关闭为止。 So they may be reused under certain circumstances and depending on the desired workflow. 因此,它们可能会在某些情况下重复使用,具体取决于所需的工作流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM