matplotlib的基本用法(十五)——主次坐标轴

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要介绍matplotlib的一些用法。

  • Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
import matplotlib.pyplot as plt

# 定义数据
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x ** 2
y2 = -1 * y1

# 定义figure
fig, ax1 = plt.subplots()
# 得到ax1的对称轴ax2
ax2 = ax1.twinx()
# 绘制图像
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b--')

# 设置label
ax1.set_xlabel('X data')
ax1.set_ylabel('Y 1', color = 'g')
ax2.set_ylabel('Y 2', color = 'b')

plt.show()
  • 结果

图像

参考资料

  1. https://www.youtube.com/user/MorvanZhou
如果有收获,可以请我喝杯咖啡!