猿问

matplotlib 以我希望它们工作的方式计算

我不明白使用 matplotlib 创建的图形是如何显示的,何时更新,何时阻塞等。


为了帮助我理解并帮助我让 matplotlib 做我特别需要的事情,谁能帮我创建一个 matplotlib.figure 包装器/几乎克隆,我们称之为 MyFigure,它的行为与下面的代码(及其注释)中描述的完全一样?


X=[0,1]

Y=[0,1]

Y2a=[0,2]

Y2b=[0,2.5]

Y3a=[0,3]

Y3b=[0,3.5]

Y4=[0,4]


fig = MyFigure() # Nothing happens

ax=fig.gca() # Nothing happens

ax.plot(X,Y) # Nothing happens

fig.show() # New window (1) appears and shows plot of X,Y; execution continues directly

ax.set_xlabel('X') # Window 1 gets X axis label

ax.set_ylabel('Y') # Window 1 gets Y axis label

fig.hide() # Window 1 disappears

time.sleep(10) # No open windows for 10 seconds 

fig.show() # Window 1 reappears and looks the same as before


fig2 = MyFigure() # Nothing happens

fig2.show() # New window (2) appears and is empty

ax2 = fig2.gca() # Window 2 shows axes

ax2.plot(X,Y2a) # Window 2 shows X,Y2a

fig2.show() # Nothing happens, could be omitted

time.sleep(60) # Long computation. In the meantime, windows 1 and 2 can still be resized and their controls (zoom etc.) can be used

ax2.plot(X,Y2b) # Window 2 shows X,Y2a as well as X,Y2b

fig2.hide() # Window 2 disappears


fig3 = MyFigure() # Nothing happens

ax3 = fig3.gca() # Nothing happens

ax3.plot(X,Y3a) # Nothing happens 

fig3.show() # Window 3 appears and shows plot of X,Y3a; execution continues

fig3.freeze() # Nothing happens, Window 3 is still open and can be manipulated

ax3.set_xlabel('X') # Nothing happens

ax3.set_ylabel('Y') # Nothing happens

fig3.thaw() # Window 3 gets X and Y axis labels 

fig3.clear() # Window 3 is still open but empty

ax3 = fig3.gca() # Window 3 shows empty axes

ax3.plot(X,Y3b) # Window 3 shows plot of X,Y3b


慕森王
浏览 163回答 2
2回答

一只萌萌小番薯

也许以下是您正在寻找的内容。由于这个问题真的很广泛,同时要求很多东西,我将把它限制在前两个例子中。这里的关键是使用plt.ion().import matplotlibmatplotlib.use("Qt5Agg")import matplotlib.pyplot as pltX=[0,1]Y=[0,1]Y2a=[0,2]Y2b=[0,2.5]Y3a=[0,3]Y3b=[0,3.5]Y4=[0,4]plt.ion()fig, ax  = plt.subplots() # Nothing happensax.plot(X,Y) # Nothing happensplt.draw()plt.pause(.1) # New window (1) appears and shows plot of X,Y; execution continues directlyax.set_xlabel('X') # Window 1 gets X axis labelax.set_ylabel('Y') # Window 1 gets Y axis labelfigManager = plt.get_current_fig_manager()figManager.window.showMinimized() # Window 1 disappearsplt.pause(10) # No open windows for 10 seconds figManager.window.showNormal() # Window 1 reappears and looks the same as beforefig2 = plt.figure() # Nothing happensfig2.show() # New window (2) appears and is emptyax2 = fig2.gca() # Window 2 shows axesax2.plot(X,Y2a) # Window 2 shows X,Y2apass # Nothing happens, could be omittedfor i in range(60*2):    plt.pause(1/2) # Long computation. In the meantime, windows 1 and 2 can still be resized and their controls (zoom etc.) can be usedax2.plot(X,Y2b) # Window 2 shows X,Y2a as well as X,Y2bfigManager = plt.get_current_fig_manager()figManager.window.showMinimized() # Window 2 disappears(1) plt.ion 在这里做什么?plt.ion()打开交互模式。这意味着可以在 GUI 内绘制图形而无需启动 GUI 事件循环。优点很明显,GUI事件循环不会阻塞后续代码;缺点是您没有事件循环 - 因此 GUI 可能很快变得无响应,您需要自行负责让它管理事件。这就是plt.pause在这种情况下的作用。可能发生的情况是,无论您使用什么来运行脚本,都已经为您打开了交互模式。在这种情况下plt.ioff(),请使用以查看差异。(2) 可以在不抓住焦点的情况下实现像 plt.pause 这样的东西吗?可能,但大多数用户希望在绘制时实际看到他们的图。所以它不是以这种方式实现的。(3)如果plt.draw没有plt.pause(.1)没有任何效果,为什么它以当前的形式存在?plt.draw()绘制图形。这在各种情况下都很有用,并且完全独立于是否ion使用。(4) fig.show 在概念上是否与 (a) 如果不存在打开窗口相同 (b) 将焦点放在窗口 (c) plt.draw();plt.pause(0.1) 上?我对此表示强烈怀疑。然而,它可能在交互模式下具有相同的面向用户的效果。(5) 在进行实际计算(与 plt.pause 相反)时,真的不可能让数字响应吗?不可以。您可以在事件循环内运行计算。您也可以在线程中运行它。最后,您可以在计算过程中调用plt.pause()或fig.canvas.flush_events()重复。(6)figManager.window.showMinimized对我没有任何影响;那是因为这是操作系统相关的吗?它不应该依赖于操作系统。但它肯定依赖于后端。这就是我将后端设置在代码之上的原因。
随时随地看视频慕课网APP

相关分类

Python
我要回答