matplotlib 我如何分别显示数字?

wbrvyc0a  于 2023-06-23  发布在  其他
关注(0)|答案(7)|浏览(146)

假设我在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()
mzillmmw

mzillmmw1#

当然。使用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))
vngu2lb8

vngu2lb82#

在Matplotlib 1.0.1之前的版本中,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()可以多次调用(大多数后端)。

06odsfpq

06odsfpq3#

我想我来晚了一点但是...在我看来,你需要的是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()显示“当前”堆栈中的任何内容。只有figure.show在使用GUI后端(例如否则,我认为你需要真正深入研究matplotlib的核心来monkeypatch一个解决方案。
记住这一点最(所有?)plt.* 函数只是figure和axes方法的快捷方式和别名。它们对于顺序编程非常有用,但是如果您计划以更复杂的方式使用它们,您很快就会发现阻塞墙。

bnl4lu3b

bnl4lu3b4#

您可能需要阅读有关interactive usage of Matplotlib的内容。但是,如果你要构建一个应用程序,你应该使用API并将图形嵌入到你选择的GUI工具包的窗口中(参见examples/embedding_in_tk.py等)。

wqlqzqxt

wqlqzqxt5#

以上解决方案似乎都不适用于我的情况,matplotlib 3.1.0Python 3.7.3。要么这两个数字都出现在调用show()上,要么没有出现在上面发布的不同答案中。
基于@Ivan的回答,并从here中得到提示,以下内容似乎对我很有效:

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

q5iwbnjs6#

作为@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
w6mmgewl

w6mmgewl7#

截至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()可防止图形立即打开和关闭。

相关问题