0

I read some of the related issues for avoiding warning "Adding an axes using the same arguments as previous axes currently reuses the earlier instance." but I could not apply it in the code below:

The code below generates 10 rectangles which each rectangle has a different color on its edges, coord is an array with size [5,2,10] which contains the coordinates of 10 rectangles.

import matplotlib.pyplot as plt

pixels = 600
my_dpi = 100
num_geo=5
    
for i in range(10):
    fig = plt.figure(num_geo,figsize=( pixels/my_dpi,  pixels/my_dpi),facecolor='k', dpi=my_dpi)  
    plt.axes([0,0,1,1])
    rectangle = plt.Rectangle((-300, -300), 600, 600, fc='k')
    plt.gca().add_patch(rectangle)

    plt.plot([coord[0][0][i], coord[4][0][i]], [coord[0][1][i], coord[4][1][i]], color=str(a1), lw=8, antialiased=True)
    plt.axis('off')
    plt.axis([-300,300,-300,300])                  
    plt.savefig('fig/%d.png' % i, dpi=my_dpi)   
    plt.close()  

John
  • 37
  • 6
  • Does this answer your question? [How to plot in multiple subplots](https://stackoverflow.com/q/31726643/7758804) – Trenton McKinney Aug 11 '21 at 15:46
  • I read that post but still, I am a bit confused about how to apply it in my case. – John Aug 11 '21 at 16:00
  • `num_geo` never changes, so you keep opening figure 5 and adding the same axes to it. You get the warning because matplotlib assumes that is not what you want to do. Above, you have not explained _what_ you want to do. – Jody Klymak Aug 11 '21 at 16:11
  • I updated the code. – John Aug 11 '21 at 16:28

0 Answers0