figures, subplots, axes 和 ticks 对象¶

figures, axes 和 ticks 的关系¶

这些对象的关系可以用下面的图来表示:

示例图像:

<img src="./artists_figure.png" width = "600" height = "400" alt="图1" align=left />

具体结构:

<img src="./artists_tree.png" width = "600" height = "400" alt="图2" align=left />

figure 对象¶

figure 对象是最外层的绘图单位,默认是以 1 开始编号(MATLAB 风格,Figure 1, Figure 2, …),可以用 plt.figure() 产生一幅图像,除了默认参数外,可以指定的参数有:

  • num - 编号
  • figsize - 图像大小
  • dpi - 分辨率
  • facecolor - 背景色
  • edgecolor - 边界颜色
  • frameon - 边框
    这些属性也可以通过 Figure 对象的 set_xxx 方法来改变。

subplot 和 axes 对象¶

subplot¶

subplot 主要是使用网格排列子图:

In [1]:

  1. %pylab inline
  2.  
  3. subplot(2,1,1)
  4. xticks([]), yticks([])
  5. text(0.5,0.5, 'subplot(2,1,1)',ha='center',va='center',size=24,alpha=.5)
  6.  
  7. subplot(2,1,2)
  8. xticks([]), yticks([])
  9. text(0.5,0.5, 'subplot(2,1,2)',ha='center',va='center',size=24,alpha=.5)
  10.  
  11. show()
  1. Populating the interactive namespace from numpy and matplotlib

06.08 figures, subplots, axes 和 ticks 对象 - 图1

更高级的可以用 gridspec 来绘图:

In [2]:

  1. import matplotlib.gridspec as gridspec
  2.  
  3. G = gridspec.GridSpec(3, 3)
  4.  
  5. axes_1 = subplot(G[0, :])
  6. xticks([]), yticks([])
  7. text(0.5,0.5, 'Axes 1',ha='center',va='center',size=24,alpha=.5)
  8.  
  9. axes_2 = subplot(G[1,:-1])
  10. xticks([]), yticks([])
  11. text(0.5,0.5, 'Axes 2',ha='center',va='center',size=24,alpha=.5)
  12.  
  13. axes_3 = subplot(G[1:, -1])
  14. xticks([]), yticks([])
  15. text(0.5,0.5, 'Axes 3',ha='center',va='center',size=24,alpha=.5)
  16.  
  17. axes_4 = subplot(G[-1,0])
  18. xticks([]), yticks([])
  19. text(0.5,0.5, 'Axes 4',ha='center',va='center',size=24,alpha=.5)
  20.  
  21. axes_5 = subplot(G[-1,-2])
  22. xticks([]), yticks([])
  23. text(0.5,0.5, 'Axes 5',ha='center',va='center',size=24,alpha=.5)
  24.  
  25. show()

06.08 figures, subplots, axes 和 ticks 对象 - 图2

axes 对象¶

subplot 返回的是 Axes 对象,但是 Axes 对象相对于 subplot 返回的对象来说要更自由一点。Axes 对象可以放置在图像中的任意位置:

In [3]:

  1. axes([0.1,0.1,.8,.8])
  2. xticks([]), yticks([])
  3. text(0.6,0.6, 'axes([0.1,0.1,.8,.8])',ha='center',va='center',size=20,alpha=.5)
  4.  
  5. axes([0.2,0.2,.3,.3])
  6. xticks([]), yticks([])
  7. text(0.5,0.5, 'axes([0.2,0.2,.3,.3])',ha='center',va='center',size=16,alpha=.5)
  8.  
  9. show()

06.08 figures, subplots, axes 和 ticks 对象 - 图3

In [4]:

  1. axes([0.1,0.1,.5,.5])
  2. xticks([]), yticks([])
  3. text(0.1,0.1, 'axes([0.1,0.1,.8,.8])',ha='left',va='center',size=16,alpha=.5)
  4.  
  5. axes([0.2,0.2,.5,.5])
  6. xticks([]), yticks([])
  7. text(0.1,0.1, 'axes([0.2,0.2,.5,.5])',ha='left',va='center',size=16,alpha=.5)
  8.  
  9. axes([0.3,0.3,.5,.5])
  10. xticks([]), yticks([])
  11. text(0.1,0.1, 'axes([0.3,0.3,.5,.5])',ha='left',va='center',size=16,alpha=.5)
  12.  
  13. axes([0.4,0.4,.5,.5])
  14. xticks([]), yticks([])
  15. text(0.1,0.1, 'axes([0.4,0.4,.5,.5])',ha='left',va='center',size=16,alpha=.5)
  16.  
  17. show()

06.08 figures, subplots, axes 和 ticks 对象 - 图4

后面的 Axes 对象会覆盖前面的内容。

ticks 对象¶

ticks 用来注释轴的内容,我们可以通过控制它的属性来决定在哪里显示轴、轴的内容是什么等等。

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/06-matplotlib/06.08-figures,-subplots,-axes-and-ticks.ipynb