问题
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....).
回答1:
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
回答2:
Moreover if you are interested you can set the x and y axes regarding the values of the circles:
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from copy import copy
def plot_circles (Liste_circles):
if len(Liste_circles) == 0:
print('No circles to display')
else:
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111)
# On définie un fond blanc
ax.set_facecolor((1, 1, 1))
# On définie les limites des axes pour que nos cercles soient au centre de notre graphique
# --> xmin sera le plus petit des x de tous les cercles - 2 fois le plus grand des rayons
# --> xmax sera le plus grand des x de tous les cercles + 2 fois le plus grand des rayons
# --> ymin sera le plus petit des y de tous les cercles - 2 fois le plus grand des rayons
# --> ymax sera le plus grand des y de tous les cercles + 2 fois le plus grand des rayons
L_x = [c.center[0] for c in Liste_circles]
L_y = [c.center[1] for c in Liste_circles]
L_r = [c.radius for c in Liste_circles]
min_x=min(L_x); max_x=max(L_x);min_y=min(L_y); max_y=max(L_y);max_r=max(L_r)
xmin = min_x - 2*max_r
xmax = max_x + 2*max_r
ymin = min_y - 2*max_r
ymax = max_y + 2*max_r
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
for c in Liste_circles:
new_c=copy(c)
ax.add_patch(new_c)
plt.show()
C1=Circle(xy=(3, 4), radius=2, fill=False, color='g')
C2=Circle(xy=(6, 3), radius=4, fill=False, color='b')
C3=Circle(xy=(7, 2), radius=4, fill=False, color='r')
来源:https://stackoverflow.com/questions/47554753/can-not-put-single-artist-in-more-than-one-figure