简体   繁体   English

什么时候需要 plt.show() 来显示绘图,什么时候不需要?

[英]When is plt.show() required to show a plot and when is it not?

Since the following code will show a plot without plt.show() , what is the point of plt.show() ?由于以下代码将显示一个没有plt.show() ,那么plt.show()什么意义?

Please tell me when plt.show() is required as this would allow me to appreciate the intricacy of matplotlib better.请告诉我什么时候需要plt.show() ,因为这会让我更好地理解 matplotlib 的复杂性。

NB: I'm using this in Spyder (Anaconda)注意:我在 Spyder (Anaconda) 中使用这个

import matplotlib.pyplot as plt
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])

To require or not required depending on where your script is.需要或不需要取决于您的脚本所在的位置。

There are 2 contexts.有2个上下文。

  1. Matplotlib is used in a terminal or scripts, plt.show() is a must. Matplotlib 用于终端或脚本, plt.show() 是必须的。

  2. Matplotlib is used in a IPython shell or a notebook (ex: Kaggle), plt.show() is unnecessary. Matplotlib 用于 IPython shell 或笔记本(例如:Kaggle),不需要 plt.show()。

It seems either you are in an interactive mode or are using a JuPyter notebook, in both the cases plt.show() being rendered redundant (check the bold highlighted doc below)似乎您处于交互模式或正在使用 JuPyter 笔记本,在这两种情况下plt.show()被渲染为多余(检查下面粗体突出显示的文档)

From the official docs来自官方文档

Display a figure.显示一个图形。 When running in ipython with its pylab mode, display all figures and return to the ipython prompt.当以 pylab 模式在 ipython 中运行时,显示所有图形并返回 ipython 提示符。

In non-interactive mode, display all figures and block until the figures have been closed;在非交互模式下,显示所有图形并阻止直到图形关闭; in interactive mode it has no effect unless figures were created prior to a change from non-interactive to interactive mode (not recommended).在交互模式下,除非在从非交互模式更改为交互模式(不推荐)之前创建图形,否则它无效 In that case it displays the figures but does not block.在这种情况下,它会显示数字但不会阻止。

A single experimental keyword argument, block, may be set to True or False to override the blocking behavior described above.可以将单个实验性关键字参数 block 设置为 True 或 False 以覆盖上述阻止行为。

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

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