简介
本文将继续介绍matplotlib绘图的基础操作。主要涉及到坐标轴axes的相关方法。
范围
通过坐标轴实例方法 set_xlim 或者 set_ylim 限制坐标轴的范围:
1 2 |
>>>ax.set_xlim([min, max]) >>>ax.set_ylim([min, max]) |
或者使用 axis('tight') 自动调整到比较紧凑的范围:
1 |
>>>ax.axis('tight') |
代码演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>>>import numpy as np >>>import matplotlib.pyplot as plt >>>x = np.array([0, 1, 2, 3]) >>>fig, ax = plt.subplots(1, 3, figsize=(12, 4)) >>>ax[0].plot(x, x ** 2) >>>ax[1].plot(x, x ** 2) >>>ax[1].set_xlim([0, 5]) >>>ax[1].set_ylim([0, 12]) >>>ax[2].plot(x, x ** 2) >>>ax[2].axis('tight') >>>plt.show() |
在这里要介绍下 fig, axes = plt.subplots(1, 3, figsize=(12, 4)) ,能够创建1行3列的子坐标轴(也就是三幅子图),默认会自动添加画布,返回当前的画布 fig 和该画布中包含所有坐标轴的数组,如何绘制子图,在后面我们还会详细介绍。得到如下图形:
其中,中间的图是通过 set_xlim 和 set_ylim 方法设置的范围,右边的图是通过 axis('tight') 设置的范围。
对数刻度
通过 set_xscale 和 set_yscale 方法能够设定刻度,传入 'log' 能够设定对数刻度:
1 2 |
>>>ax.set_xscale('log') >>>ax.set_yscale('log') |
代码演示:
1 2 3 4 5 6 7 8 |
>>>x = np.arange(50) >>>fig = plt.figure() >>>ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) >>>ax.plot(x, x ** 2) >>>ax.set_yscale('log') >>>plt.show() |
得到如下图形:
坐标轴标号和符号
通过 set_xticks 和 set_yticks 方法能够自定义设置要显示的坐标轴刻度,通过 set_xticklabels 和 set_yticklabels 方法能够在坐标轴刻度加上符号显示:
1 2 |
>>>ax.set_xticks([1, 2, 3, 4, 5]) >>>ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) |
代码演示:
1 2 3 4 5 6 7 8 9 |
>>>x = np.array([0.5, 1.5, 2.5, 3.5]) >>>fig = plt.figure() >>>ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) >>>ax.plot(x, x ** 2) >>>ax.set_xticks([0, 1, 2, 3, 4]) >>>ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) >>>plt.show() |
得到如下图形:
可以看出使用刻度符号 ['a', 'b', 'c', 'd', 'e'] 会替换刻度值 [0, 1, 2, 3, 4] 。
坐标轴刻度与坐标轴标签间距
通过坐标轴实例的 xaxis.labelpad 和 yaxis.labelpad 方法能够调节坐标轴刻度与坐标轴标签的间距:
1 2 |
>>>ax.xaxis.labelpad = 5 >>>ax.yaxis.labelpad = 5 |
代码演示:
1 2 3 4 5 6 7 8 9 10 |
>>>x = np.arange(5) >>>fig = plt.figure() >>>ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) >>>ax.plot(x, x ** 2) >>>ax.set_xlabel('X') >>>ax.set_ylabel('Y') >>>ax.xaxis.labelpad = 2 >>>plt.show() |
得到如下图形:
可以看出x轴标签与x轴的位置发生了变化。
调整坐标轴位置
当使用 plt.subplots 方法创建的坐标轴,我们还可以使用下面的方法调整坐标轴的位置:
1 |
>>>fig.subplots_adjust(left=0.1, right=0.8, bottom=0.1, top= 0.8) |
代码演示:
1 2 3 4 5 6 7 |
>>>x = np.arange(5) >>>fig, ax = plt.subplots(1, 1) >>>ax.plot(x, x ** 2) >>>fig.subplots_adjust(left=0.1, right=0.6, bottom=0.1, top= 0.6) >>>plt.show() |
得到如下图形:
网格
我们还可以通过 grid 方法在图中加入网格线:
1 |
>>>ax.grid(color='r', ls='dashed', lw=0.5, alpha=0.8) |
代码演示:
1 2 3 4 5 6 7 |
>>>x = np.arange(5) >>>fig, ax = plt.subplots(1, 1) >>>ax.plot(x, x ** 2) >>>ax.grid(color='r', ls='dashed', lw=0.5, alpha=0.8) >>>plt.show() |
得到如下图形:
轴属性
通过轴实例的 spines 方法,能够控制上下左右四个坐标轴的属性。比如可以设置颜色,线宽等:
1 2 3 4 |
>>>ax.spines['top'].set_color('red') >>>ax.spines['bottom'].set_linewidth('1.5') >>>ax.spines['right'].set_color('none') # 隐藏坐标轴 >>>ax.yaxis.tick_left() # 只在y轴左边设置刻度 |
接下来就一个设置坐标原点到(0, 0)的绘图过程为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
>>>fig = plt.figure() >>>ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # 删除上右坐标轴 >>>ax.spines['top'].set_color('none') >>>ax.spines['right'].set_color('none') >>>ax.xaxis.set_ticks_position('bottom') >>>ax.spines['bottom'].set_position(('data', 0)) >>>ax.yaxis.set_ticks_position('left') >>>ax.spines['left'].set_position(('data', 0)) >>>x = np.linspace(-5, 5, 50) >>>ax.plot(x, x ** 2) >>>plt.show() |
得到如下图形:
双坐标轴
通过 twinx 和 twiny 可以设置双坐标轴:
1 2 |
>>>ax1.plot() >>>ax2 = ax1.twinx() # 共用x轴,双y轴 |
代码演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
>>>x = np.arange(5) >>>fig = plt.figure() >>>ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) >>>ax1.plot(x, x ** 2, color='b') >>>ax1.set_ylabel('left label', color='blue') >>>ax2 = ax1.twinx() >>>ax2.plot(x, x ** 3, color='r') >>>ax2.set_ylabel('right label', color='red') # 设置轴标颜色 >>>for i in ax1.get_yticklabels(): i.set_color('blue') >>>for j in ax2.get_yticklabels(): j.set_color('red') >>>plt.show() |
得到如下图形:
总结
本文详细介绍了坐标轴的相关方法,可以看出通过代码方式实现坐标轴设置的灵活性很高,实际工作中要结合自己的需求去绘图。