【发布时间】:2016-11-19 19:47:35
【问题描述】:
我想在绘制的图形上添加更细粒度的网格。问题是所有示例都需要访问轴对象。 我想将特定的网格添加到已经绘制的图形中(从 ipython 内部)。
如何在 ipython 中访问当前图形和轴?
【问题讨论】:
标签: python matplotlib ipython axis figure
我想在绘制的图形上添加更细粒度的网格。问题是所有示例都需要访问轴对象。 我想将特定的网格添加到已经绘制的图形中(从 ipython 内部)。
如何在 ipython 中访问当前图形和轴?
【问题讨论】:
标签: python matplotlib ipython axis figure
plt.gcf()获取当前数字
plt.gca() 获取当前坐标轴
【讨论】:
以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>]
显示figure 1;得到它的句柄:
In [47]: f=plt.figure(1)
In [48]: f
Out[48]: <matplotlib.figure.Figure at 0xb17acb2c>
及其轴列表:
In [49]: f.axes
Out[49]: [<matplotlib.axes._subplots.AxesSubplot at 0xb091198c>]
为当前(也是唯一的)轴打开网格:
In [51]: a=f.axes[0]
In [52]: a.grid(True)
我有一段时间没有使用 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>]
还有gcf 和gca(获取当前图形/轴)。如果我没记错的话,和 MATLAB 中的一样。
In [68]: plt.gca()
Out[68]: <matplotlib.axes._subplots.AxesSubplot at 0xb17a35cc>
In [66]: plt.gcf()
Out[66]: <matplotlib.figure.Figure at 0xb091eeec>
(这些在侧边栏链接中使用:Matplotlib.pyplot - Deactivate axes in figure. /Axis of figure overlap with axes of subplot)
【讨论】: