- 1.设置方法
- 2.用函数autoscale()调整坐标轴范围
import matplotlib.pyplot as plt import matplotlib as mpl 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) # set (2,2) subplots fig,ax = plt.subplots(2,2) # set chart of each subplot ax1 = plt.subplot(221) ax1.plot(x1,y1) ax2 = plt.subplot(222) ax2.plot(x2,y2) ax3 = plt.subplot(223) ax3.scatter(x3,y3) ax4 = plt.subplot(224,sharex=ax1) ax4.scatter(x4,y4) plt.show()2.用函数autoscale()调整坐标轴范围
autoscale()进行坐标轴的自适应调整,以使图形可以非常紧凑的填充绘图区域。调用签名是
autoscale(enable=True,axis=“both”,tight=True)
enable:进行坐标轴范围的自适应调整
axis:使x,y轴都进行自适应调整
tight:让坐标轴的范围调整到数据的范围上
例如将子区3的x轴范围调整到数据范围上,只需在子区3的代码部分增加下面的代码
import matplotlib.pyplot as plt import matplotlib as mpl 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) # set (2,2) subplots fig,ax = plt.subplots(2,2) # set chart of each subplot ax1 = plt.subplot(221) ax1.plot(x1,y1) ax2 = plt.subplot(222) ax2.plot(x2,y2) ax3 = plt.subplot(223) plt.autoscale(enable=True,axis="both",tight=True) ax3.scatter(x3,y3) ax4 = plt.subplot(224,sharex=ax1) ax4.scatter(x4,y4) plt.show()



