matplotlib 绘图和 imshow
matplotlib 的情节和 imshow 的行为让我感到困惑。
import matplotlib as mpl
import matplotlib.pyplot as plt
如果我在调用 plt.imshow(i)
之前调用 plt.show()
,则会出现错误。如果我在调用 plt.show()
之前调用 plt.imshow(i)
,那么一切都会正常运行。但是,如果我关闭第一个打开的图形,然后调用 plt.imshow(i),则会显示一个新图形,而无需调用 plt.show()
。
有人可以解释一下吗?
The behavior of matplotlib's plot and imshow is confusing to me.
import matplotlib as mpl
import matplotlib.pyplot as plt
If I call plt.show()
prior to calling plt.imshow(i)
, then an error results. If I call plt.imshow(i)
prior to calling plt.show()
, then everything works perfectly. However, if I close the first figure that gets opened, and then call plt.imshow(i)
, a new figure is displayed without ever calling plt.show()
.
Can someone explain this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
plt.show()
显示图形(并进入您正在使用的任何 gui 后端的主循环)。在绘制图形并希望看到它们显示之前,不应调用它。plt.imshow()
在当前图窗上绘制图像(如果当前没有图窗,则创建一个图窗)。在绘制任何内容之前调用plt.show()
没有任何意义。如果您想显式创建一个新图窗,请使用 plt.figure()。除非你以类似于 ipython 的 pylab 模式的方式运行代码,否则这种情况不会发生,其中 gui 后端的主循环将在单独的线程中运行...
一般来说, plt.show() 将是最后一行你的脚本。 (或者无论何时你想停下来想象你所做的情节,都会被调用。)
希望这更有意义。
plt.show()
displays the figure (and enters the main loop of whatever gui backend you're using). You shouldn't call it until you've plotted things and want to see them displayed.plt.imshow()
draws an image on the current figure (creating a figure if there isn't a current figure). Callingplt.show()
before you've drawn anything doesn't make any sense. If you want to explictly create a new figure, useplt.figure()
.That wouldn't happen unless you're running the code in something similar to ipython's pylab mode, where the gui backend's main loop will be run in a separate thread...
Generally speaking, plt.show() will be the last line of your script. (Or will be called whenever you want to stop and visualize the plot you've made, at any rate.)
Hopefully that makes some more sense.