我有点迷失,试图在Subplots集合中绘制相同的补丁集合。我的代码目前是:
patches = []
for i in range(len(A)):
rect = mpatches.Rectangle([A[i], 20], (B[i]-A[i]), 10, ec="none")
patches.append(rect)
collection = PatchCollection(patches, match_original=True)
fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(221)
ax1.plot(SOME STUFF)
ax1.add_collection(collection)
ax2 = fig.add_subplot(222)
ax2.plot(SOME STUFF)
ax2.add_collection(collection)
如果我只是尝试在ax1中绘制集合,它可以正常工作,但是只要我添加代码行将集合添加到ax2中,我就会得到一个用子图生成的图形并且数据正确显示但是没有补丁。
本
答案 0 :(得分:1)
我测试并得到同样的问题。它就像一个集合与一个轴“链接”。 您可能需要创建两个集合:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.collections as collections
# Position
P = np.random.uniform(0,1, (10,2))
# Size (width x height)
S = np.random.uniform(0.1,.2, (10,2))
patches = []
for i in range(len(P)):
rect = mpatches.Rectangle(P[i], S[i,0], S[i,1])
patches.append(rect)
collection1 = collections.PatchCollection(patches)
collection1.set_edgecolors('none')
collection1.set_facecolors('.75')
collection2 = collections.PatchCollection(patches)
collection2.set_edgecolors('none')
collection2.set_facecolors('.75')
fig = plt.figure(figsize=(16,8))
ax1 = fig.add_subplot(121, aspect=1)
ax1.add_collection(collection1)
ax2 = fig.add_subplot(122, aspect=1)
ax2.add_collection(collection2)
plt.show()