Matplotlib: Adding an axes using the same arguments as a previous axes

前端 未结 5 2043
死守一世寂寞
死守一世寂寞 2020-11-27 17:22

I want to plot data, in two different subplots. After plotting, I want to go back to the first subplot and plot an additional dataset in it. However, when I do so I get this

5条回答
  •  感动是毒
    2020-11-27 18:15

    Using plt.subplot(1,2,1) creates a new axis in the current figure. The deprecation warning is telling that in a future release, when you call it a second time, it will not grab the previously created axis, instead it will overwrite it.

    You can save a reference to the first instance of the axis by assigning it to a variable.

    plt.figure()
    # keep a reference to the first axis
    ax1 = plt.subplot(1,2,1)
    ax1.plot(Data)
    
    # and a reference to the second axis
    ax2 = plt.subplot(1,2,2)
    ax2.plot(Data)
    
    # reuse the first axis
    ax1.plot(Data+1)
    

提交回复
热议问题