matplotlib图和imshow [英] matplotlib plot and imshow

查看:137
本文介绍了matplotlib图和imshow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matplotlib的图和imshow的行为令我感到困惑.

The behavior of matplotlib's plot and imshow is confusing to me.

import matplotlib as mpl
import matplotlib.pyplot as plt

如果在调用plt.imshow(i)之前先调用plt.show(),则会导致错误.如果我在调用plt.show()之前先调用plt.imshow(i),那么一切都会正常进行.但是,如果我关闭第一个打开的图形,然后调用plt.imshow(i),则会显示一个新图形,而无需调用plt.show().

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

有人可以解释吗?

推荐答案

如果我在致电之前致电plt.show() plt.imshow(i),然后导致错误. 如果我在之前致电plt.imshow(i) 调用plt.show(),然后一切 效果很好.

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.

plt.show()显示该图(并进入您正在使用的任何gui后端的主循环).在您绘制事物并希望看到它们显示之前,您不应该调用它.

plt.show() displays the figure (and enters the mainloop 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()在当前图形上绘制图像(创建图形时没有当前图形).在绘制任何内容之前调用plt.show()没有任何意义.如果要显式创建新图形,请使用plt.figure().

plt.imshow() draws an image on the current figure (creating a figure is there isn't a current figure). Calling plt.show() before you've drawn anything doesn't make any sense. If you want to explictly create a new figure, use plt.figure().

...一个新的数字从未显示 调用plt.show().

...a new figure is displayed without ever calling plt.show().

除非您以类似于ipython的pylab模式的方式运行代码,否则不会发生,其中gui后端的mainloop将在单独的线程中运行...

That wouldn't happen unless you're running the code in something similar to ipython's pylab mode, where the gui backend's mainloop will be run in a seperate thread...

通常来说,plt.show()将是脚本的最后一行. (或者,无论何时要停止并以可视化方式可视化所绘制的图,都会调用它.)

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

希望这更有意义.

这篇关于matplotlib图和imshow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆