How do I get multiple subplots in matplotlib?

后端 未结 6 862
自闭症患者
自闭症患者 2020-11-22 06:49

I am a little confused about how this code works:

fig, axes = plt.subplots(nrows=2, ncols=2)
plt.show()

How does the fig, axes work in this

6条回答
  •  名媛妹妹
    2020-11-22 07:03

    • You can also unpack the axes in the subplots call

    • And set whether you want to share the x and y axes between the subplots

    Like this:

    import matplotlib.pyplot as plt
    fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
    ax1.plot(range(10), 'r')
    ax2.plot(range(10), 'b')
    ax3.plot(range(10), 'g')
    ax4.plot(range(10), 'k')
    plt.show()
    

提交回复
热议问题