[英]Matplotlib: how to set the current figure?
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.
我想使用matplotlib显示2个数字,然后以交互方式使用它们。 I create the figures with:
我用以下方法创建数字:
import matplotlib
import pylab as pl
f1 = pl.figure()
f2 = pl.figure()
and can use the MATLAB-like pyplot interface to plot and draw in both figures. 并可以使用类似MATLAB的pyplot界面在两个图中进行绘制和绘制。 With
用
current_figure = pl.gcf()
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. 我可以确定pyplot界面的当前活动图形,具体取决于我单击的图形。现在,我想使用pyplot界面在第一个图形上绘制一些东西,但是当前图形可以是其中任意一个。 So is there something like
所以有什么像
pl.set_current_figure(figure)
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)
(我知道我可以使用面向对象的界面,但是对于交互式的东西,仅使用诸如plot(x,y)之类的命令就更好了)
You can simply set figure f1
as the new current figure with: 您可以使用以下命令简单地将图
f1
设置为新的当前图:
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 : The pyplot equivalent of pylab.figure()
is matplotlib.pyplot.figure()
. PS :
pylab.figure()
的pyplot等效于matplotlib.pyplot.figure()
。
Give each figure a number: 给每个图一个数字:
f1 = pl.figure(1)
f2 = pl.figure(2)
# use f2
pl.figure(1) # make f1 active again
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.