matplotlib: can I create AxesSubplot objects, then add them to a Figure instance?

后端 未结 3 1000
一整个雨季
一整个雨季 2020-11-22 12:08

Looking at the matplotlib documentation, it seems the standard way to add an AxesSubplot to a Figure is to use Figure.add_subplo

3条回答
  •  醉酒成梦
    2020-11-22 12:41

    The following shows how to "move" an axes from one figure to another. This is the intended functionality of @JoeKington's last example, which in newer matplotlib versions is not working anymore, because axes cannot live in several figures at once.

    You would first need to remove the axes from the first figure, then append it to the next figure and give it some position to live in.

    import matplotlib.pyplot as plt
    
    fig1, ax = plt.subplots()
    ax.plot(range(10))
    ax.remove()
    
    fig2 = plt.figure()
    ax.figure=fig2
    fig2.axes.append(ax)
    fig2.add_axes(ax)
    
    dummy = fig2.add_subplot(111)
    ax.set_position(dummy.get_position())
    dummy.remove()
    plt.close(fig1)
    
    plt.show()
    

提交回复
热议问题