第七章:共享绘图区域的坐标轴

1、共享单一绘图区域的坐标轴

 1 import  matplotlib
 2 import  matplotlib.pyplot as plt
 3 import numpy as np
 4 
 5 # 显示中文标识
 6 matplotlib.rcParams["font.sans-serif"] = ["SimHei"]
 7 matplotlib.rcParams["axes.unicode_minus"] = False
 8 
 9 # 返回一个画布对象fig和一个坐标轴实例ax。
10 fig,ax1 = plt.subplots()
11 
12 # 自变量参数
13 t = np.arange(0.05,10.0,0.01)
14 
15 s1 = np.exp(t)                                  # 指数函数
16 ax1.plot(t,s1,c="b",ls="-")                     # 在ax1当中绘制指数函数,颜色为蓝色
17 ax1.set_xlabel("X坐标轴")                       # 设置X轴标签
18 ax1.set_ylabel("以e为底的指数函数",color="b")   # 设置Y轴标签
19 ax1.tick_params(axis="y",color="b")             # 设置Y坐标轴的刻度为蓝色
20 
21 # 创建一个与ax1共享X轴的坐标轴实例ax2,但是Y轴不共享
22 ax2 = ax1.twinx()
23 
24 s2 = np.cos(t**2)                               # 余弦函数
25 ax2.plot(t,s2,c="r",ls=":")                     # 在ax2当中绘制指数函数,颜色为红色
26 ax2.set_ylabel("余弦函数",color="r")            # 设置Y轴标签,因为X轴已经共享了
27 ax2.tick_params(axis="y",color="r")             # 设置Y坐标轴的刻度为红色
28 
29 plt.show()

 

 2、共享不同子区绘图区域的坐标轴

 1 import  matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x1 = np.linspace(0,2*np.pi,400)
 5 y1 = np.cos(x1**2)
 6 
 7 x2 = np.linspace(0.01,10,100)
 8 y2 = np.sin(x2)
 9 
10 x3 = np.random.rand(100)
11 y3 = np.linspace(0,3,100)
12 
13 x4 = np.arange(0,6,0.5)
14 y4 = np.power(x4,3)
15 
16 fig,ax = plt.subplots(2,2)
17 
18 ax1 = ax[0,0]
19 ax1.plot(x1,y1)
20 
21 ax2 = ax[0,1]
22 ax2.plot(x2,y2)
23 
24 ax3 = ax[1,0]
25 ax3.scatter(x3,y3)
26 
27 ax4 = ax[1,1]
28 ax4.scatter(x4,y4)
29 
30 plt.show()

(1) 当fig,ax = plt.subplots(2,2,sharex="all")时。

若sharex="all",4幅图的横坐标都采用了同一个坐标范围;若sharey="all",4幅图的纵坐标都采用了同一个坐标范围

 (2) 当fig,ax = plt.subplots(2,2,sharex="none")时等价于fig,ax = plt.subplots(2,2)本身,不共享任何坐标。

 (3) 当fig,ax = plt.subplots(2,2,sharex="row")时,每一行的子分区共享横坐标。

 (4) 当fig,ax = plt.subplots(2,2,sharex="col")时,每一列的子分区共享横坐标。

 

 3、将共享坐标轴的子区之间的空隙去掉

 1 import  matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x = np.linspace(0.0,10.0,200)
 5 y1 = np.cos(x)*np.sin(x)
 6 y2 = np.exp(-x)*np.sin(x)
 7 y3 = 3*np.sin(x)
 8 y4 = np.power(x,0.5)
 9 
10 # 通过sharex="all"实现X轴坐标共享
11 fig,(ax1,ax2,ax3,ax4) = plt.subplots(4,1,sharex="all")
12 
13 # 将4幅图水平区域的空隙去掉
14 fig.subplots_adjust(hspace=0)
15 
16 ax1.plot(x,y1,ls="-",lw=2,c="b")
17 ax1.set_yticks(np.arange(-0.6,0.7,0.2))
18 ax1.set_ylim(-0.7,0.7)
19 
20 ax2.plot(x,y2,ls="-",lw=2,c="r")
21 ax2.set_yticks(np.arange(-0.05,0.36,0.1))
22 ax2.set_ylim(-0.1,0.4)
23 
24 ax3.plot(x,y3,ls="-",lw=2,c="g")
25 ax3.set_yticks(np.arange(-3,4,1))
26 ax3.set_ylim(-3.5,3.5)
27 
28 ax4.plot(x,y4,ls="-",lw=2,c="c")
29 ax4.set_yticks(np.arange(0.0,3.6,0.5))
30 ax4.set_ylim(0.0,4.0)
31 
32 plt.show()

 

 4、共享个别子区绘图区域的坐标轴

import  matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(0,2*np.pi,400)
y1 = np.cos(x1**2)

x2 = np.linspace(0.01,10,100)
y2 = np.sin(x2)

x3 = np.random.rand(100)
y3 = np.linspace(0,3,100)

x4 = np.arange(0,6,0.5)
y4 = np.power(x4,3)

fig,ax = plt.subplots(2,2)

ax1 = plt.subplot(221)
ax1.plot(x1,y1)

ax2 = plt.subplot(222)
ax1.plot(x2,y2)

ax3 = plt.subplot(223)
ax3.scatter(x3,y3)

ax4 = plt.subplot(224,sharex=ax1)    # 与第一个子区的横坐标共享
ax4.scatter(x4,y4)

plt.show()

 

posted @ 2019-10-13 15:32  懒惰的小松鼠  阅读(1104)  评论(0编辑  收藏  举报