-
Notifications
You must be signed in to change notification settings - Fork 26
/
chapter-7.py
108 lines (65 loc) · 2.09 KB
/
chapter-7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# chapter 7
# 共享坐标轴
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
t = np.arange(0.05, 10.0, 0.01)
s1 = np.exp(t)
s2 = np.cos(t)
fig, ax = plt.subplots() # 默认一行一列
ax.plot(t, s1, c='b',ls='--',label='$\exp(x)$')
ax.set_xlabel('x-axis') # 设置x坐标轴
ax.set_ylabel('以e为底数的指数函数', color='b') # 设置y坐标轴
ax.tick_params('y', colors='b') # y坐标轴的刻度
## 关键,共享x坐标轴
ax2 = ax.twinx()
ax2.plot(t, s2, c='g', ls=":",label='$\cos(x)$')
ax2.set_ylabel('余弦函数',color='r') # 设置y坐标轴
ax2.tick_params('y', colors='r')
plt.legend()
plt.title('共享x轴')
plt.show()
### 共享不同子图的坐标轴
# 调整subplots中的sharex或sharey参数
# sharex与sharey的取值共有4中,row\col\all\none,其中all和none分别等同与true和false
x1 = np.linspace(0,2*np.pi,100)
y1 = np.sin(x1)
x2 = np.linspace(0,4,100)
y2 = np.random.randn(100)
x3 = np.linspace(0,2*np.pi,100)
y3 = np.cos(x3**2)
x4 = np.linspace(0,4,10)
y4 = np.power(x4,2)
fig, ax = plt.subplots(2,2,sharex=False,sharey=False)
ax[0,0].plot(x1,y1)
ax[0,1].scatter(x2,y2)
ax[1,0].plot(x3,y3)
ax[1,1].scatter(x4,y4)
plt.show()
## 去掉子图之间的间隙
# fig.subplots_adjust(hspace=0, # 去掉水平间隙
# vspace=0) # 去掉竖直间隙
# 情况三 共享个别子区域的坐标轴
### 共享不同子图的坐标轴
# 调整subplots中的sharex或sharey参数
# sharex与sharey的取值共有4中,row\col\all\none,其中all和none分别等同与true和false
x1 = np.linspace(-10,2*np.pi,100)
y1 = np.sin(x1)
x2 = np.linspace(0,4,100)
y2 = np.random.randn(100)
x3 = np.linspace(0,2*np.pi,100)
y3 = np.cos(x3**2)
x4 = np.linspace(0,4,10)
y4 = np.power(x4,2)
# fig, ax = plt.subplots(2,2)
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223,sharey=ax2) # 2与3共享y轴
ax4 = plt.subplot(224,sharex=ax1) # 1与4 共享x轴
ax1.plot(x1,y1)
ax2.scatter(x2,y2)
ax3.plot(x3,y3)
ax4.scatter(x4,y4)
plt.show()