【问题标题】:Matplotlib: how to set the current figure?Matplotlib:如何设置当前图形?
【发布时间】:2011-12-20 15:45:00
【问题描述】:

希望这是一个简单的问题,但我目前无法弄清楚。我想使用 matplotlib 显示 2 个数字,然后以交互方式使用它们。我使用以下方法创建图形:

import matplotlib
import pylab as pl

f1 = pl.figure()
f2 = pl.figure()

并且可以使用类似 MATLAB 的 pyplot 接口在两个图形中进行绘图和绘制。与

current_figure = pl.gcf()

我可以确定 pyplot 界面的当前活动图形,这取决于我单击了哪个图形。现在我想用 pyplot 界面在第一个图形上绘制一些东西,但当前图形可以是它们中的任何一个。那么有没有类似的东西

pl.set_current_figure(figure)

或任何解决方法? (我知道我可以使用面向对象的界面,但对于交互式内容,只需使用 plot(x, y) 之类的命令就更好了)

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以简单地将图形f1 设置为新的当前图形:

    pl.figure(f1.number)
    

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

    pl.figure("Share values")
    # ... some plots ...
    pl.figure("Profits")
    # ... some plots ...
    
    pl.figure("Share values")  # Selects the first figure again
    

    事实上,数字“数字”可以是字符串,可以说比简单数字更明确。

    PSpylab.figure() 的 pyplot 等效项是 matplotlib.pyplot.figure()

    【讨论】:

    • 有没有使用 matplotlib.figure.Figure 而不是 pylab 的好方法??
    • @tylerthemiler:是的,但那是matplotlib.pyplot.figure()
    • 如果你想对轴做同样的事情,只需使用pylab.sca(my_axis)(“设置当前轴”)。
    【解决方案2】:

    给每个图一个数字:

    f1 = pl.figure(1)
    f2 = pl.figure(2)
    # use f2
    pl.figure(1) # make f1 active again
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 2017-07-08
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多