如何在matplotlib中单独显示数字?

Fed*_*oni 49 python matplotlib

假设我在matplotlib中有两个数字,每个数字有一个数字:

import matplotlib.pyplot as plt

f1 = plt.figure()
plt.plot(range(0,10))
f2 = plt.figure()
plt.plot(range(10,20))
Run Code Online (Sandbox Code Playgroud)

然后我一次性展示

plt.show()
Run Code Online (Sandbox Code Playgroud)

有没有办法单独显示它们,即只显示f1

或者更好:我如何单独管理数字,如下面的"如意"代码(不起作用):

f1 = plt.figure()
f1.plot(range(0,10))
f1.show()
Run Code Online (Sandbox Code Playgroud)

Ste*_*joa 43

当然.添加Axes使用add_subplot.(已编辑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()
Run Code Online (Sandbox Code Playgroud)

或者,使用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))
Run Code Online (Sandbox Code Playgroud)

  • @Steve:我不明白你的解决方案如何回答这个问题.弗雷德里科问如何只显示f1.你的代码显示了f1和f2,从我所知道的,与Frederico的他不想做的事情非常相似.编辑中有什么东西丢失了吗? (5认同)

Eri*_*got 25

使用版本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
Run Code Online (Sandbox Code Playgroud)

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

我不认为Matplotlib提供了一种创建图形并可选择显示它的机制; 这意味着figure()将显示创建的所有数字.如果您只需要按顺序显示单独的图形(在同一窗口中或不在同一窗口中),您可以在上面的代码中执行.

现在,在简单的情况下,对于一些Matplotlib后端,上述解决方案可能就足够了.有些后端很好,即使你没有打电话,也可以让你与第一个数字进行交互show().但是,据我所知,他们不一定要好.最强大的方法是在一个单独的线程中启动每个图形绘制show(),每个线程中都有一个final .我相信这基本上就是IPython所做的.

上面的代码大部分时间都应该足够了.

PS:现在,使用Matplotlib版本1.0.1+,show()可以多次调用(大多数后端).


小智 7

我想我参加聚会有点晚了,但是...我认为,您需要的是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.
Run Code Online (Sandbox Code Playgroud)

在这种情况下,plt.show()显示“当前”堆栈中的任何内容。仅当使用GUI后端(例如Qt4Agg)时,才可以指定fig.show()。否则,我认为您将需要深入挖掘matplotlib的精髓,以轻松解决问题。

请记住,大多数(全部?)plt。*函数只是图形和轴方法的快捷方式和别名。它们对于顺序编程非常有用,但是如果您打算以更复杂的方式使用它们,则会很快发现阻塞墙。