ipython:访问当前图形()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38415774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:46:34  来源:igfitidea点击:

ipython : get access to current figure()

pythonmatplotlibipythonaxisfigure

提问by sten

I want to add more fine grained grid on a plotted graph. The problem is all of the examples require access to the axis object. I want to add specific grid to already plotted graph (from inside ipython).

我想在绘制的图形上添加更细粒度的网格。问题是所有示例都需要访问轴对象。我想向已经绘制的图形添加特定的网格(从 ipython 内部)。

How do I gain access to the current figure and axis in ipython ?

如何访问 ipython 中的当前图形和轴?

回答by runDOSrun

plt.gcf()to get current figure

plt.gcf()获取当前数字

plt.gca()to get current axis

plt.gca()获取当前轴

回答by hpaulj

With the plt?example (assuming ipython --pylab)

plt?例子(假设ipython --pylab

In [44]: x=np.arange(0,5,.1)
In [45]: y=np.sin(x)
In [46]: plt.plot(x,y)
Out[46]: [<matplotlib.lines.Line2D at 0xb09418cc>]

displays figure 1; get its handle with:

显示figure 1; 得到它的处理:

In [47]: f=plt.figure(1)

In [48]: f
Out[48]: <matplotlib.figure.Figure at 0xb17acb2c>

and a list of its axes:

及其轴列表:

In [49]: f.axes
Out[49]: [<matplotlib.axes._subplots.AxesSubplot at 0xb091198c>]

turn the grid on for the current (and only) axis:

打开当前(且唯​​一)轴的网格:

In [51]: a=f.axes[0]
In [52]: a.grid(True)

I haven't used the plt in a while, so found this stuff by just making the plot and searching the tab completion and ? for likely stuff. I'm pretty sure this is also available in the pltdocumentation.

我有一段时间没有使用 plt,所以只需制作绘图并搜索选项卡完成和 ? 对于可能的东西。我很确定这也可以在plt文档中找到。

Or you can create the figure first, and hang on to its handle

或者你可以先创建图形,然后抓住它的手柄

In [53]: fig=plt.figure()
In [55]: ax1=fig.add_subplot(2,1,1)
In [56]: ax2=fig.add_subplot(2,1,2)

In [57]: plt.plot(x,y)
Out[57]: [<matplotlib.lines.Line2D at 0xb12ed5ec>]

In [58]: fig.axes
Out[58]: 
[<matplotlib.axes._subplots.AxesSubplot at 0xb0917e2c>,
 <matplotlib.axes._subplots.AxesSubplot at 0xb17a35cc>]

And there's gcfand gca(get current figure/axis). Same as in MATLAB if my memory is correct.

还有gcfgca(获取当前图形/轴)。如果我的记忆是正确的,则与 MATLAB 中的相同。

In [68]: plt.gca()
Out[68]: <matplotlib.axes._subplots.AxesSubplot at 0xb17a35cc>
In [66]: plt.gcf()
Out[66]: <matplotlib.figure.Figure at 0xb091eeec>

(these are used in the sidebar link: Matplotlib.pyplot - Deactivate axes in figure. /Axis of figure overlap with axes of subplot)

(这些在侧边栏链接中使用:Matplotlib.pyplot - 停用图中的轴。/图轴与子图轴重叠