如何单独显示数字?
假设我在 matplotlib 中有两个图形,每个图形一个图:
import matplotlib.pyplot as plt
f1 = plt.figure()
plt.plot(range(0,10))
f2 = plt.figure()
plt.plot(range(10,20))
然后我在一个镜头中显示两个图形
plt.show()
是否有一种方法可以单独显示它们,即仅显示 f1
?
或者更好的是:我如何单独管理这些数字,就像下面的“一厢情愿”代码一样(这不起作用):
f1 = plt.figure()
f1.plot(range(0,10))
f1.show()
Say that I have two figures in matplotlib, with one plot per figure:
import matplotlib.pyplot as plt
f1 = plt.figure()
plt.plot(range(0,10))
f2 = plt.figure()
plt.plot(range(10,20))
Then I show both in one shot
plt.show()
Is there a way to show them separately, i.e. to show just f1
?
Or better: how can I manage the figures separately like in the following 'wishful' code (that doesn't work):
f1 = plt.figure()
f1.plot(range(0,10))
f1.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当然。使用
add_subplot
添加一个Axes
。 (编辑import
。)(编辑show
。)或者,使用
add_axes
。Sure. Add an
Axes
usingadd_subplot
. (Editedimport
.) (Editedshow
.)Alternatively, use
add_axes
.对于版本 1.0.1 之前的 Matplotlib,
show()
每个程序只应调用一次,即使它似乎在某些环境(某些后端、某些平台上等)中工作。相关的绘图函数实际上是
draw()
:重要的是要认识到
show()
是一个无限循环,旨在处理各种图形中的事件(调整大小等) .)。请注意,原则上,如果您在脚本开头调用matplotlib.ion()
,则对draw()
的调用是可选的(我已经看到这在某些平台上失败了)和后端,不过)。我不认为 Matplotlib 提供了创建图形并可选择显示它的机制;这意味着将显示使用
figure()
创建的所有图形。如果您只需要顺序显示单独的图形(无论是否在同一窗口中),您可以像上面的代码一样进行操作。现在,上述解决方案在简单情况下以及某些 Matplotlib 后端可能就足够了。即使您没有调用show() 。我相信这本质上就是 IPython 所做的。
show()
,某些后端也足以让您与第一个图形进行交互。但是,据我了解,他们不一定是友善的。最可靠的方法是在单独的线程中启动每个图形绘制,并在每个线程中使用最终的大多数情况下,上面的代码应该足够了。
PS:现在,使用 Matplotlib 1.0.1+ 版本,可以多次调用
show()
(对于大多数后端)。With Matplotlib prior to version 1.0.1,
show()
should only be called once per program, even if it seems to work within certain environments (some backends, on some platforms, etc.).The relevant drawing function is actually
draw()
:It is important to recognize that
show()
is an infinite loop, designed to handle events in the various figures (resize, etc.). Note that in principle, the calls todraw()
are optional if you callmatplotlib.ion()
at the beginning of your script (I have seen this fail on some platforms and backends, though).I don't think that Matplotlib offers a mechanism for creating a figure and optionally displaying it; this means that all figures created with
figure()
will be displayed. If you only need to sequentially display separate figures (either in the same window or not), you can do like in the above code.Now, the above solution might be sufficient in simple cases, and for some Matplotlib backends. Some backends are nice enough to let you interact with the first figure even though you have not called
show()
. But, as far as I understand, they do not have to be nice. The most robust approach would be to launch each figure drawing in a separate thread, with a finalshow()
in each thread. I believe that this is essentially what IPython does.The above code should be sufficient most of the time.
PS: now, with Matplotlib version 1.0.1+,
show()
can be called multiple times (with most backends).我想我参加聚会有点晚了但是...
在我看来,你需要的是 matplotlib 的面向对象的 API。在 matplotlib 1.4.2 中并使用 IPython 2.4.1 和 Qt4Agg 后端,我可以执行以下操作:
在这种情况下 plt.show() 显示“当前”堆栈中的任何内容。仅当您使用 GUI 后端(例如 Qt4Agg)时,您才可以指定figure.show()。否则,我认为您将需要真正深入研究 matplotlib 的内部来猴子修补解决方案。
请记住,大多数(全部?) plt.* 函数只是图形和轴方法的快捷方式和别名。它们对于顺序编程非常有用,但是如果您计划以更复杂的方式使用它们,您很快就会发现障碍墙。
I think I am a bit late to the party but...
In my opinion, what you need is the object oriented API of matplotlib. In matplotlib 1.4.2 and using IPython 2.4.1 with Qt4Agg backend, I can do the following:
In this case plt.show() shows anything in the "current" stack. You can specify figure.show() ONLY if you are using a GUI backend (e.g. Qt4Agg). Otherwise, I think you will need to really dig down into the guts of matplotlib to monkeypatch a solution.
Remember that most (all?) plt.* functions are just shortcuts and aliases for figure and axes methods. They are very useful for sequential programing, but you will find blocking walls very soon if you plan to use them in a more complex way.
也许您需要阅读Matplotlib 的交互式使用。但是,如果您要构建应用程序,则应该使用 API 和将图形嵌入到您选择的 GUI 工具包的窗口中(参见
examples/embedding_in_tk.py
等)。Perhaps you need to read about interactive usage of Matplotlib. However, if you are going to build an app, you should be using the API and embedding the figures in the windows of your chosen GUI toolkit (see
examples/embedding_in_tk.py
, etc).对于我的情况,上述解决方案似乎都不起作用,使用
matplotlib 3.1.0
和Python 3.7.3
。要么两个数字都在调用show()
时显示,要么在上面发布的不同答案中都没有显示。基于@Ivan的回答,并从此处获取提示,以下内容似乎对我来说效果很好:
None of the above solutions seems to work in my case, with
matplotlib 3.1.0
andPython 3.7.3
. Either both the figures show up on callingshow()
or none show up in different answers posted above.Building upon @Ivan's answer, and taking hint from here, the following seemed to work well for me:
作为@arpanmangal,上述解决方案对我不起作用(
matplotlib 3.0.3
,python 3.5.2
)。似乎不建议在图中使用
.show()
,例如figure.show()
,因为此方法不管理 GUI 事件循环,因此该图仅简要显示。 (参见 figure.show() 文档)。但是,我没有找到任何其他方法来仅显示数字。在我的解决方案中,我通过使用单击事件来防止图形立即关闭。我们不必关闭图窗 — 关闭图窗会将其删除。
我提出两个选择:
-
waitforbuttonpress(timeout=-1)
点击图形时会关闭图形窗口,因此我们无法使用缩放等窗口功能。-
ginput(n=-1,show_clicks=False)
将等到我们关闭窗口,但它会释放一个错误:-。例子:
As @arpanmangal, the solutions above do not work for me (
matplotlib 3.0.3
,python 3.5.2
).It seems that using
.show()
in a figure, e.g.,figure.show()
, is not recommended, because this method does not manage a GUI event loop and therefore the figure is just shown briefly. (See figure.show() documentation). However, I do not find any another way to show only a figure.In my solution I get to prevent the figure for instantly closing by using click events. We do not have to close the figure — closing the figure deletes it.
I present two options:
-
waitforbuttonpress(timeout=-1)
will close the figure window when clicking on the figure, so we cannot use some window functions like zooming.-
ginput(n=-1,show_clicks=False)
will wait until we close the window, but it releases an error :-.Example:
截至 2020 年 11 月,为了一次显示一个图窗,请执行以下操作:
调用
input()
阻止图窗立即打开和关闭。As of November 2020, in order to show one figure at a time, the following works:
The call to
input()
prevents the figure from opening and closing immediately.