简体   繁体   English

Matplotlib:如何设置当前数字?

[英]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() . PSpylab.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.

相关问题 如何设置matplotlib数字的边距? - How to set the margins for a matplotlib figure? 如何在 Python matplotlib 中设置图形的大小 - How to set the size of a figure in Python matplotlib 你如何在Python的matplotlib中获得当前的数字? - How do you get the current figure number in Python's matplotlib? 使用%matplotlib内联的ipython笔记本时如何保持当前的数字? - How to keep the current figure when using ipython notebook with %matplotlib inline? matplotlib 中经常提到“电流图”,但实际的电流图是什么? - The "Current Figure" is often mentioned in matplotlib, but what actually IS the current figure? 如何在matplotlib中为一个数字设置本地rcParams或rcParams - how to set local rcParams or rcParams for one figure in matplotlib 使用Matplotlib绘制小图-如何设置图形大小? - Drawing small-multiple charts with matplotlib - how to set figure size? 如何直接为matplotlib图形背景色设置alpha通道值 - How to directly set alpha channel value for matplotlib figure background colour 如何在matplotlib python 2.6.6中在我的图形中的X轴上设置“步骤”? - How to set “step” on axis X in my figure in matplotlib python 2.6.6? 如何使用matplotlib和SVM算法向图形(数据集)添加边界? - How to add a boundary to a figure (data set) using matplotlib and SVM algorithm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM