gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:12:03 57 4
gpt4 key购买 nike

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

(假设我们将 pyplot 导入为 from matplotlib import pyplot as plt)

最佳答案

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

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

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

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

有关更多信息,请参阅 Matplotlib 文档“Interactive figures”。

关于python - matplotlib 中 plt.draw() 和 plt.show() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23141452/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com