如何单独显示数字?

发布于 2024-08-24 04:29:40 字数 420 浏览 4 评论 0原文

假设我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泅渡 2024-08-31 04:29:40

当然。使用add_subplot添加一个Axes。 (编辑import。)(编辑show。)

import matplotlib.pyplot as plt
f1 = plt.figure()
f2 = plt.figure()
ax1 = f1.add_subplot(111)
ax1.plot(range(0,10))
ax2 = f2.add_subplot(111)
ax2.plot(range(10,20))
plt.show()

或者,使用add_axes

ax1 = f1.add_axes([0.1,0.1,0.8,0.8])
ax1.plot(range(0,10))
ax2 = f2.add_axes([0.1,0.1,0.8,0.8])
ax2.plot(range(10,20))

Sure. Add an Axes using add_subplot. (Edited import.) (Edited show.)

import matplotlib.pyplot as plt
f1 = plt.figure()
f2 = plt.figure()
ax1 = f1.add_subplot(111)
ax1.plot(range(0,10))
ax2 = f2.add_subplot(111)
ax2.plot(range(10,20))
plt.show()

Alternatively, use add_axes.

ax1 = f1.add_axes([0.1,0.1,0.8,0.8])
ax1.plot(range(0,10))
ax2 = f2.add_axes([0.1,0.1,0.8,0.8])
ax2.plot(range(10,20))
紫南 2024-08-31 04:29:40

对于版本 1.0.1 之前的 Matplotlib,show() 每个程序只应调用一次,即使它似乎在某些环境(某些后端、某些平台上等)中工作。

相关的绘图函数实际上是draw()

import matplotlib.pyplot as plt

plt.plot(range(10))  # Creates the plot.  No need to save the current figure.
plt.draw()  # Draws, but does not block
raw_input()  # This shows the first figure "separately" (by waiting for "enter").

plt.figure()  # New window, if needed.  No need to save it, as pyplot uses the concept of current figure
plt.plot(range(10, 20))
plt.draw()
# raw_input()  # If you need to wait here too...

# (...)

# Only at the end of your program:
plt.show()  # blocks

重要的是要认识到show()是一个无限循环,旨在处理各种图形中的事件(调整大小等) .)。请注意,原则上,如果您在脚本开头调用 matplotlib.ion() ,则对 draw() 的调用是可选的(我已经看到这在某些平台上失败了)和后端,不过)。

我不认为 Matplotlib 提供了创建图形并可选择显示它的机制;这意味着将显示使用 figure() 创建的所有图形。如果您只需要顺序显示单独的图形(无论是否在同一窗口中),您可以像上面的代码一样进行操作。

现在,上述解决方案在简单情况下以及某些 Matplotlib 后端可能就足够了。即使您没有调用 show(),某些后端也足以让您与第一个图形进行交互。但是,据我了解,他们不一定是友善的。最可靠的方法是在单独的线程中启动每个图形绘制,并在每个线程中使用最终的show() 。我相信这本质上就是 IPython 所做的。

大多数情况下,上面的代码应该足够了。

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

import matplotlib.pyplot as plt

plt.plot(range(10))  # Creates the plot.  No need to save the current figure.
plt.draw()  # Draws, but does not block
raw_input()  # This shows the first figure "separately" (by waiting for "enter").

plt.figure()  # New window, if needed.  No need to save it, as pyplot uses the concept of current figure
plt.plot(range(10, 20))
plt.draw()
# raw_input()  # If you need to wait here too...

# (...)

# Only at the end of your program:
plt.show()  # blocks

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 to draw() are optional if you call matplotlib.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 final show() 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).

人事已非 2024-08-31 04:29:40

我想我参加聚会有点晚了但是...
在我看来,你需要的是 matplotlib 的面向对象的 API。在 matplotlib 1.4.2 中并使用 IPython 2.4.1 和 Qt4Agg 后端,我可以执行以下操作:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1) # Creates figure fig and add an axes, ax.
fig2, ax2 = plt.subplots(1) # Another figure

ax.plot(range(20)) #Add a straight line to the axes of the first figure.
ax2.plot(range(100)) #Add a straight line to the axes of the first figure.

fig.show() #Only shows figure 1 and removes it from the "current" stack.
fig2.show() #Only shows figure 2 and removes it from the "current" stack.
plt.show() #Does not show anything, because there is nothing in the "current" stack.
fig.show() # Shows figure 1 again. You can show it as many times as you want.

在这种情况下 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:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1) # Creates figure fig and add an axes, ax.
fig2, ax2 = plt.subplots(1) # Another figure

ax.plot(range(20)) #Add a straight line to the axes of the first figure.
ax2.plot(range(100)) #Add a straight line to the axes of the first figure.

fig.show() #Only shows figure 1 and removes it from the "current" stack.
fig2.show() #Only shows figure 2 and removes it from the "current" stack.
plt.show() #Does not show anything, because there is nothing in the "current" stack.
fig.show() # Shows figure 1 again. You can show it as many times as you want.

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.

吾性傲以野 2024-08-31 04:29:40

也许您需要阅读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).

月亮是我掰弯的 2024-08-31 04:29:40

对于我的情况,上述解决方案似乎都不起作用,使用 matplotlib 3.1.0Python 3.7.3。要么两个数字都在调用 show() 时显示,要么在上面发布的不同答案中都没有显示。

基于@Ivan的回答,并从此处获取提示,以下内容似乎对我来说效果很好:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1) # Creates figure fig and add an axes, ax.
fig2, ax2 = plt.subplots(1) # Another figure

ax.plot(range(20)) #Add a straight line to the axes of the first figure.
ax2.plot(range(100)) #Add a straight line to the axes of the first figure.

# plt.close(fig) # For not showing fig
plt.close(fig2) # For not showing fig2
plt.show()

None of the above solutions seems to work in my case, with matplotlib 3.1.0 and Python 3.7.3. Either both the figures show up on calling show() 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:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1) # Creates figure fig and add an axes, ax.
fig2, ax2 = plt.subplots(1) # Another figure

ax.plot(range(20)) #Add a straight line to the axes of the first figure.
ax2.plot(range(100)) #Add a straight line to the axes of the first figure.

# plt.close(fig) # For not showing fig
plt.close(fig2) # For not showing fig2
plt.show()
孤独陪着我 2024-08-31 04:29:40

作为@arpanmangal,上述解决方案对我不起作用(matplotlib 3.0.3python 3.5.2)。

似乎不建议在图中使用 .show(),例如 figure.show(),因为此方法不管理 GUI 事件循环,因此该图仅简要显示。 (参见 figure.show() 文档)。但是,我没有找到任何其他方法来仅显示数字。

在我的解决方案中,我通过使用单击事件来防止图形立即关闭。我们不必关闭图窗 — 关闭图窗会将其删除。

我提出两个选择:
- waitforbuttonpress(timeout=-1) 点击图形时会关闭图形窗口,因此我们无法使用缩放等窗口功能。
- ginput(n=-1,show_clicks=False) 将等到我们关闭窗口,但它会释放一个错误:-。

例子:

import matplotlib.pyplot as plt

fig1, ax1 = plt.subplots(1) # Creates figure fig1 and add an axes, ax1
fig2, ax2 = plt.subplots(1) # Another figure fig2 and add an axes, ax2

ax1.plot(range(20),c='red') #Add a red straight line to the axes of fig1.
ax2.plot(range(100),c='blue') #Add a blue straight line to the axes of fig2.

#Option1: This command will hold the window of fig2 open until you click on the figure
fig2.waitforbuttonpress(timeout=-1) #Alternatively, use fig1

#Option2: This command will hold the window open until you close the window, but
#it releases an error.
#fig2.ginput(n=-1,show_clicks=False) #Alternatively, use fig1

#We show only fig2
fig2.show() #Alternatively, use fig1

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:

import matplotlib.pyplot as plt

fig1, ax1 = plt.subplots(1) # Creates figure fig1 and add an axes, ax1
fig2, ax2 = plt.subplots(1) # Another figure fig2 and add an axes, ax2

ax1.plot(range(20),c='red') #Add a red straight line to the axes of fig1.
ax2.plot(range(100),c='blue') #Add a blue straight line to the axes of fig2.

#Option1: This command will hold the window of fig2 open until you click on the figure
fig2.waitforbuttonpress(timeout=-1) #Alternatively, use fig1

#Option2: This command will hold the window open until you close the window, but
#it releases an error.
#fig2.ginput(n=-1,show_clicks=False) #Alternatively, use fig1

#We show only fig2
fig2.show() #Alternatively, use fig1
征﹌骨岁月お 2024-08-31 04:29:40

截至 2020 年 11 月,为了一次显示一个图窗,请执行以下操作:

import matplotlib.pyplot as plt

f1, ax1 = plt.subplots()
ax1.plot(range(0,10))
f1.show()
input("Close the figure and press a key to continue")
f2, ax2 = plt.subplots()
ax2.plot(range(10,20))
f2.show()
input("Close the figure and press a key to continue")

调用 input() 阻止图窗立即打开和关闭。

As of November 2020, in order to show one figure at a time, the following works:

import matplotlib.pyplot as plt

f1, ax1 = plt.subplots()
ax1.plot(range(0,10))
f1.show()
input("Close the figure and press a key to continue")
f2, ax2 = plt.subplots()
ax2.plot(range(10,20))
f2.show()
input("Close the figure and press a key to continue")

The call to input() prevents the figure from opening and closing immediately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文