课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
20分钟

创建多个 subplot

如果你想创建网格中的许多subplot,旧式风格的代码非常繁琐:

  #旧式风格
  fig=plt.figure()
  ax1=fig.add_subplot(221)
  ax2=fig.add_subplot(222,sharex=ax1,sharey=ax1)
  ax3=fig.add_subplot(223,sharex=ax1,sharey=ax1)
  ax4=fig.add_subplot(224,sharex=ax1,sharey=ax1)

新式风格的代码直接利用pyplot.subplots()函数一次性创建:

    #新式风格的代码
    fig,((ax1,ax2),(ax3,ax4))=plt.subplots(2,2,sharex=True,sharey=True)
    ax1.plot(...)
    ax2.plot(...)
    ...

它创建了Figure和对应所有网格SubPlot。你也可以不去解包而直接:

    #新式风格的代码
    fig,((ax1,ax2),(ax3,ax4))=plt.subplots(2,2,sharex=True,sharey=True)
    ax1.plot(...)
    ax2.plot(...)
    ...

返回的axs是一个nrows*ncolsarray,支持numpy的索引。