matplotlib中subplots的用法

1.matplotlib中如果只画一张图的话,可以直接用pyplot,一般的做法是:

  1. import matplotlib.pyplot as plt
  2. plt.figure(figsize=(20,8),dpi=90) # 设置画布大小及像素
  3. plt.xticks()  # 设置x坐标刻度
  4. plt.yticks() # 设置y坐标刻度
  5. plt.xlabel() # 设置x坐标名
  6. plt.ylabel() # 设置y坐标名
  7. plt.title() # 设置主题
  8. plt.plot() # 画图
  9. plt.legend(loc='') # 显示图例

2.当需要在一张图中画两个坐标系,需要用到subplots,具体用法如下:

  1. import matplotlib.pyplot as plt
  2. fig,ax = plt.subplots(nrows=n,ncols=m,figsize=(20,8)) # fig为返回的图像,ax为返回的坐标系(为一个数组)
  3. ax[0],set_xticks() #设置各个坐标系的刻度
  4. ax[0].set_yticks() # 需要注意,这里每个坐标系都需要单独设置刻度,坐标轴名称,主题,图例等
  5. ax[0].set_xlabel()
  6. ax[0].set_ylabel()
  7. ax[0].set_title()
  8. ax[0].legend(loc='')
  9. ax[0].plot()

猜你喜欢

转载自www.cnblogs.com/maodoudou/p/11717342.html