Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not put single artist in more than one figure

Here is my issue.

I create a function that plot a list of circles. I need to plot my circle C1 first with a circle C2 then with C3.... until C40.

import matplotlib.pyplot as plt 
from matplotlib.patches import Circle

def plot_circle(Liste_circles):


    fig = plt.figure(figsize=(5,5))   
    ax = fig.add_subplot(111)

    # On définie un fond blanc
    ax.set_facecolor((1, 1, 1))

    ax.set_xlim(-5, 15)
    ax.set_ylim(-6, 12)    

    for c in Liste_circles:
        ax.add_patch(c)

    plt.show()

Now I create C1:

C1=Circle(xy=(3, 4), radius=2, fill=False, color='g')

An finally I try to plot it.
The first plot worked:

C2=Circle(xy=(6, 3), radius=4, fill=False, color='b')
plot_circle([C1,C2])

The second one failed:

C3=Circle(xy=(7, 2), radius=4, fill=False, color='b')
plot_circle([C1,C3])

with the error:

RuntimeError: Can not put single artist in more than one figure

I can make it worked by doing:

C1=Circle(xy=(3, 4), radius=2, fill=False, color='g')
C3=Circle(xy=(7, 2), radius=4, fill=False, color='b')
plot_circle([C1,C3])

How can I do to plot my circle C1 with 40 other circles without having to recreate C1 each time? (My program took 10min to create C1 throught a complicated algorithme, I cannot recreate it at each of the 40 plot....).

like image 552
Anneso Avatar asked Oct 18 '25 18:10

Anneso


1 Answers

Here is how to make it work: just do a copy of the circle and plot the copy:

First import copy:

from copy import copy

then, instead of doing:

for c in Liste_circles:
    ax.add_patch(c)

we have to do:

for c in Liste_circles:
   new_c=copy(c)
   ax.add_patch(new_c)

This way we won't plot the same circle (= the same artist) but its copy

like image 132
Anneso Avatar answered Oct 21 '25 21:10

Anneso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!