Matplotlib:如何设置当前数字? [英] Matplotlib: how to set the current figure?

查看:100
本文介绍了Matplotlib:如何设置当前数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个简单的问题,但目前无法确定.我想使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆