import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x=np.linspace(-np.pi,np.pi,256)
y1 = np.sin(x)
y2 = np.cos(x)
ax_one = plt.subplot(233)
ax_two = plt.subplot(236,sharex=ax_one)
ax_one.plot(x,y1)
ax_two.plot(x,y2)
plt.show()
没有成功共享x轴
figure,(ax1,ax2,ax3,ax4,ax5,ax6) = plt.subplots(2,3,sharex=True)
ax3.plot(x,y1)
ax6.plot(x,y2)
必须要在定义的每个子图里填充内容
使用fig,ax ()
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-np.pi,np.pi,500)
y1 = np.sin(x)
y2 = np.cos(x)
fig,ax = plt.subplots(2,3,sharex =True)
ax1 = ax[0,2]
ax1.plot(x,y1)
ax2 = ax[1,2]
ax2.plot(x,y2)
plt.show()
可以只选择想要的子图填充
subplot方法属于API绘图,而add_subplot方法为面向对象绘图;使用subplot方法自动生产画布与轴域,而add_subplot方法需要先添加画布figure,再在画布上添加轴域,add_subplot返回的即为子图的轴域。