Python matplotlib 中 plt.draw() 和 plt.show() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23141452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:19:46  来源:igfitidea点击:

Difference between plt.draw() and plt.show() in matplotlib

pythonmatplotlib

提问by

I was wondering why some people put a plt.draw()into their code before the plt.show(). For my code, the behavior of the plt.draw()didn't seem to change anything about the output. I did a search on the internet but couldn't find anything useful.

我想知道为什么有些人plt.draw()plt.show(). 对于我的代码, 的行为plt.draw()似乎没有改变输出的任何内容。我在互联网上进行了搜索,但找不到任何有用的东西。

(assuming we imported pyplotas from matplotlib import pyplot as plt)

(假设我们导入pyplotfrom matplotlib import pyplot as plt

采纳答案by Ffisegydd

plt.show()will display the current figure that you are working on.

plt.show()将显示您正在处理的当前图形。

plt.draw()will re-draw the figure. This allows you to work in interactive mode and, should you have changed your data or formatting, allow the graph itself to change.

plt.draw()将重新绘制图形。这允许您在交互模式下工作,并且如果您更改了数据或格式,则允许图表本身更改。

The plt.drawdocs state:

plt.draw文档说明:

This is used in interactive mode to update a figure that has been altered using one or more plot object method calls; it is not needed if figure modification is done entirely with pyplot functions, if a sequence of modifications ends with a pyplot function, or if matplotlib is in non-interactive mode and the sequence of modifications ends with show() or savefig().

这在交互模式下用于更新已使用一个或多个绘图对象方法调用更改的图形;如果图形修改完全使用 pyplot 函数完成,如果修改序列以 pyplot 函数结束,或者如果 matplotlib 处于非交互模式并且修改序列以 show() 或 savefig() 结束,则不需要。

This seems to suggest that using plt.draw()before plt.show()when not in interactive mode will be redundant the vast majority of the time. The only time you may need it is if you are doing some very strange modifications that don't involve using pyplot functions.

这似乎表明,plt.draw()plt.show()大多数情况下,在未处于交互模式时使用before将是多余的。您可能需要它的唯一时间是您正在进行一些不涉及使用 pyplot 函数的非常奇怪的修改。

Refer to the Matplotlib FAQ, "What is interactive mode?" for more information.

有关更多信息,请参阅 Matplotlib 常见问题解答,“什么是交互模式?”。