Matplotlib:如何设置当前数字?

Ale*_*der 72 python matplotlib

希望这是一个简单的问题,但我现在无法弄明白.我想使用matplotlib来显示2个数字,然后以交互方式使用它们.我创建了数字:

import matplotlib
import pylab as pl

f1 = pl.figure()
f2 = pl.figure()
Run Code Online (Sandbox Code Playgroud)

并且可以使用类似MATLAB的pyplot接口绘制和绘制两个图形.同

current_figure = pl.gcf()
Run Code Online (Sandbox Code Playgroud)

我可以确定pyplot界面的当前活动数字,具体取决于我点击的数字.现在我想用pyplot接口绘制第一个数字,但当前数字可以是其中之一.所以有类似的东西

pl.set_current_figure(figure)
Run Code Online (Sandbox Code Playgroud)

或任何解决方法?(我知道我可以使用面向对象的界面但是对于只使用plot(x,y)等命令的交互式东西要好得多)

Eri*_*got 81

你可以简单地将figure设置f1为新的当前数字:

pl.figure(f1.number)
Run Code Online (Sandbox Code Playgroud)

另一种选择是给图形命名(或数字),这可能有助于使代码更容易阅读:

pl.figure("Share values")
# ... some plots ...
pl.figure("Profits")
# ... some plots ...

pl.figure("Share values")  # Selects the first figure again
Run Code Online (Sandbox Code Playgroud)

实际上,数字"数字"可以是字符串,可以说比简单数字更明确.

PS:本pyplot相当于pylab.figure()matplotlib.pyplot.figure().

  • 有没有使用 matplotlib.figure.Figure 而不是 pylab 的好方法来做到这一点? (2认同)

Hoa*_*Tam 14

给每个数字一个数字:

f1 = pl.figure(1)
f2 = pl.figure(2)
# use f2
pl.figure(1) # make f1 active again
Run Code Online (Sandbox Code Playgroud)