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

前端 未结 5 2036
死守一世寂寞
死守一世寂寞 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 17:49

    I had the same problem. I used to have the following code that raised the warning:

    (note that the variable Image is simply my image saved as numpy array)

    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.figure(1)  # create new image
    plt.title("My image")  # set title
    # initialize empty subplot
    AX = plt.subplot()  # THIS LINE RAISED THE WARNING
    plt.imshow(Image, cmap='gist_gray')  # print image in grayscale
    ...  # then some other operations
    

    and I solved it, modifying like this:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig_1 = plt.figure(1)  # create new image and assign the variable "fig_1" to it
    AX = fig_1.add_subplot(111)  # add subplot to "fig_1" and assign another name to it
    AX.set_title("My image")  # set title
    AX.imshow(Image, cmap='gist_gray')  # print image in grayscale
    ...  # then some other operations
    

提交回复
热议问题