ipython:访问当前的数字()

ste*_*ten 16 python axis matplotlib ipython figure

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

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

run*_*run 45

plt.gcf() 得到目前的数字

plt.gca() 获得当前轴


hpa*_*ulj 9

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>]
Run Code Online (Sandbox Code Playgroud)

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

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

In [48]: f
Out[48]: <matplotlib.figure.Figure at 0xb17acb2c>
Run Code Online (Sandbox Code Playgroud)

以及它的轴列表:

In [49]: f.axes
Out[49]: [<matplotlib.axes._subplots.AxesSubplot at 0xb091198c>]
Run Code Online (Sandbox Code Playgroud)

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

In [51]: a=f.axes[0]
In [52]: a.grid(True)
Run Code Online (Sandbox Code Playgroud)

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

或者您可以先创建图形,然后挂起它的手柄

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>]
Run Code Online (Sandbox Code Playgroud)

还有的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>
Run Code Online (Sandbox Code Playgroud)

(这些用于侧栏链接:Matplotlib.pyplot - 停用图中的轴./图的轴与子图的轴重叠)