因此,我有一个for
循环,用于在图形的子图中绘制生物。我正在使用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 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
对象。
谢谢您的帮助
答案 0 :(得分:1)
如上所述,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()