在循环 returns 中向轴添加补丁 a ValueError

Adding patches to axes in loop returns a ValueError

所以,我有一个 for 循环,在图形的子图中绘制 creatures。我正在使用 this 进行子图安排。这是我的代码摘录:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
import numpy as np
from grid_strategy import strategies
from shapely.geometry import MultiLineString
from descartes.patch import PolygonPatch


fig = plt.figure()
space = [
    Circle((2, 2), radius=5, color='red',  alpha=np.random.uniform(0, 1)),
    Circle((-2, 2), radius=5, color='red',  alpha=np.random.uniform(0, 1)),
    Circle((2, -2), radius=5, color='red',  alpha=np.random.uniform(0, 1)),
]

lines = [
    [
        [0.0, 0.0],
        [0.007963267107332634, 9.999996829318349],
        [-9.992024050168064, 10.015923358483217]
    ],
    [
        [-9.992024050168064, 10.015923358483217],
        [-5.547622120022902, 3.3640595983538804],
        [-2.7450196155284288, 10.857084301489543]
    ],
    [
        [-2.7450196155284288, 10.857084301489543],
        [-8.83003187757613, 8.873999388852386]
    ],
    [
        [-2.7450196155284288, 10.857084301489543],
        [-1.1168525035132877, 0.9905209760275202]
    ],
    [
        [-9.992024050168064, 10.015923358483217],
        [-4.576474350758081, 15.904201668245792],
        [-9.381605405732934, 9.508053175245003]
    ],
    [
        [-9.381605405732934, 9.508053175245003],
        [-9.348197751236784, 15.907965981574565]
    ],
    [
        [-9.381605405732934, 9.508053175245003],
        [-15.091199417814586, 1.2982702172611962]
    ]
]

creature = MultiLineString(lines)

creature_patch = PolygonPatch(creature.buffer(5))

plot_spec = strategies.SquareStrategy('center').get_grid(2)

for _, sub in zip(range(2), plot_spec):
    ax = fig.add_subplot(sub)

    for line in creature:
        x, y = line.xy
        ax.plot(x, y, 'r-')

    ax.add_patch(creature_patch)
    for p in space:
        ax.add_patch(p)

ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.autoscale(axis='y')
ax.axis('equal')
plt.show()

对于 for 循环的 单次 迭代,它 有效 并输出以下内容:

其中:
for line in creature:是红线
ax.add_patch(creature_patch) 是蓝色斑点,
for p in space:是红圈

然而,一旦我想绘制多个子图,我就会收到以下错误:

  File "c:\Users\Zack\Google Drive\Studies\Meesters\Meesters\New Direction (L-Systems)\New-Direction-L-Systems-\Versions\tools\render.py", line 109, in genDraw
    ax.add_patch(p)
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 1967, in add_patch
    self._set_artist_props(p)
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 926, in _set_artist_props
    a.axes = self
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py", line 209, in axes
    raise ValueError("Can not reset the axes.  You are probably "
ValueError: Can not reset the axes.  You are probably trying to re-use an artist in more than one Axes which is not supported 

我不明白,因为 ax 变量在每次迭代时都设置为不同的 Axes 对象。

感谢您的帮助

如前所述,space 包含多个补丁。一旦将它们添加到一个轴,就不能将它们添加到另一个轴。这就是错误告诉您的内容。

解决方案是为每个轴创建一组补丁,例如:

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


fig = plt.figure()

def get_space():
    space = [
        Circle((2, 2), radius=5, color='red',  alpha=np.random.uniform(0, 1)),
        Circle((-2, 2), radius=5, color='red',  alpha=np.random.uniform(0, 1)),
        Circle((2, -2), radius=5, color='red',  alpha=np.random.uniform(0, 1)),
    ]
    return space


plot_spec = fig.add_gridspec(1,2)

for _, sub in zip(range(2), plot_spec):
    ax = fig.add_subplot(sub)

    for p in get_space():
        ax.add_patch(p)

    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax.autoscale(axis='y')
    ax.axis('equal')
plt.show()