Matplotlib:如何设置当前图形? [英] Matplotlib: how to set the current figure?
问题描述
希望这是一个简单的问题,但我目前无法弄清楚.我想使用 matplotlib 显示 2 个图形,然后以交互方式使用它们.我创建的数字:
This is hopefully a simple question but I can't figure it out at the moment. I want to use matplotlib to show 2 figures and then use them interactively. I create the figures with:
import matplotlib
import pylab as pl
f1 = pl.figure()
f2 = pl.figure()
并且可以使用类似 MATLAB 的 pyplot 界面在两个图中绘制和绘制.随着
and can use the MATLAB-like pyplot interface to plot and draw in both figures. With
current_figure = pl.gcf()
我可以确定 pyplot 界面的当前活动图形,具体取决于我单击的图形.现在我想用 pyplot 界面为第一个图形绘制一些东西,但当前图形可以是其中之一.那么有没有类似的东西
I can determine the currently active figure for the pyplot interface, depending on which figure I clicked in. Now I want to draw something to the first figure with the pyplot interface but the current figure can be either of them. So is there something like
pl.set_current_figure(figure)
或任何解决方法?(我知道我可以使用面向对象的界面,但是对于交互式的东西,只需使用像 plot(x, y) 这样的命令就更好了)
or any workaround? (I know that I can use the object oriented interface but for interactive stuff just using commands like plot(x, y) is much nicer)
推荐答案
您可以简单地将图形 f1
设置为新的当前图形:
You can simply set figure f1
as the new current figure with:
pl.figure(f1.number)
另一种选择是给数字命名(或数字),这可能有助于使代码更易于阅读:
Another option is to give names (or numbers) to figures, which might help make the code easier to read:
pl.figure("Share values")
# ... some plots ...
pl.figure("Profits")
# ... some plots ...
pl.figure("Share values") # Selects the first figure again
事实上,数字数字"可以是字符串,可以说比简单的数字更明确.
In fact, figure "numbers" can be strings, which are arguably more explicit that simple numbers.
PS:pylab.figure()
的 pyplot 等价物是 matplotlib.pyplot.figure()
.
PS: The pyplot equivalent of pylab.figure()
is matplotlib.pyplot.figure()
.
这篇关于Matplotlib:如何设置当前图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!