plt.subplots(nrowos,ncols,sharex,sharey,figsize=(4, 3),dpi=200,constrained_layout=True,**kwargs)
参数:
代码示例:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 正常显示中文字体
plt.rcParams['axes.unicode_minus'] = False # 正常显示负号
# 创建制图数据
x = np.linspace(-5, 5, 100)
y_1 = np.sin(x)
y_2 = np.cos(x)
y_3 = y_2 * 2
# sharex为True,则共享坐标轴x。同理,sharey为True,则共享坐标轴y。
fig, ax = plt.subplots(nrows=3, ncols=3,figsize=(4, 3),dpi=200,
sharex=True,sharey=True,constrained_layout=True)
ax[0,0].plot(x, y_1, c='blue', linestyle=':')
ax[1,1].plot(x, y_2, c='orange', linestyle=':')
ax[2,2].plot(x, y_3, c='r', linestyle=':')
# 调整子图形之间的纵向距离
fig.subplots_adjust(hspace=0.1)
plt.show()
添加子图共用标题:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 正常显示中文字体
plt.rcParams['axes.unicode_minus'] = False # 正常显示负号
# 创建制图数据
x = np.linspace(-5, 5, 100)
y_1 = np.sin(x)
y_2 = np.cos(x)
y_3 = y_2 * 2
# sharex为True,则共享坐标轴x。同理,sharey为True,则共享坐标轴y。
fig, ax = plt.subplots(nrows=3, ncols=3,figsize=(4, 3),dpi=200,
sharex=True,sharey=True,constrained_layout=True)
ax[0,0].plot(x, y_1, c='blue', linestyle=':')
ax[1,1].plot(x, y_2, c='orange', linestyle=':')
ax[2,2].plot(x, y_3, c='r', linestyle=':')
# 调整子图形之间的纵向距离
fig.subplots_adjust(hspace=0.1)
# 添加子图共用坐标轴标题
fig.supxlabel("x轴",fontsize=9)
fig.supylabel("y轴",fontsize=9)
fig.suptitle("示例",fontsize=9)
plt.show()
设置子图参数:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 正常显示中文字体
plt.rcParams['axes.unicode_minus'] = False # 正常显示负号
# 创建制图数据
x = np.linspace(-5, 5, 100)
y_1 = np.sin(x)
y_2 = np.cos(x)
y_3 = y_2 * 2
# sharex为True,则共享坐标轴x。同理,sharey为True,则共享坐标轴y。
fig, ax = plt.subplots(nrows=3, ncols=3,figsize=(4, 3),dpi=200,
sharex=True,sharey=True,constrained_layout=True)
ax[0,0].plot(x, y_1, c='blue', linestyle=':')
ax[0,0].set_title("Left")
ax[0,0].set_xlim(-5,5)
ax[0,0].set_ylim(-5,5)
ax[0,0].set_xlabel("X")
ax[0,0].set_ylabel("Y")
ax[0,0].set_xticks(np.arange(-5,5,2))
ax[0,0].set_yticks(np.arange(-5,5,2))
plt.show()