Luk*_*mke 7 python polygon colors matplotlib colormap
我有一个包含 10,000 多个 Matplotlib Polygon 对象的列表。每个多边形属于 20 个组中的 1 个。我想通过将每个唯一组映射到唯一颜色来区分多边形属于哪个组。
以下是我发现的一些与我的问题类似的帖子:
这些解决方案只是将随机颜色应用于列表中的每个形状。那不是我要找的。对我来说,属于特定组的每个形状都应该具有相同的颜色。有任何想法吗?
示例代码:
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
from matplotlib import pyplot as plt
patches = []
colors = []
num_polys = 10000
for i in range(num_polys):
patches.append(Polygon(poly_vertices[i], closed=True))
colors.append(poly_colors[i]) # This line is pointless, but I'm letting you know that
# I have a particular color for each polygon
fig, ax = plt.subplots()
p = PatchCollection(patches, alpha=0.25)
ax.add_collection(p)
ax.autoscale()
plt.show()
Run Code Online (Sandbox Code Playgroud)
请注意,如果您运行此代码,它将无法工作,因为尚未定义 poly_vertices 和 poly_colors。现在,假设 poly_vertices 是一个多边形顶点列表,poly_colors 是一个 RGB 颜色列表,每个列表有 10000 个条目。
例如:poly_vertices[0] = [(0, 0), (1, 0), (0, 1)], colors[0] = [1, 0, 0]
谢谢!
好的,我想出了我想要做什么。我会为可能遇到类似问题的任何人发布答案。
出于某种原因,在多边形本身中设置颜色不起作用。IE
Polygon(vertices, color=[1, 0, 0])
Run Code Online (Sandbox Code Playgroud)
不起作用。
相反,在将所有多边形添加到集合后,使用
p = PatchCollection(patches)
p.set_color([1, 0, 0])
Run Code Online (Sandbox Code Playgroud)
但我仍然想按颜色对多边形进行分组。因此我需要添加多个 PatchCollections —— 每个组类型一个!
我原来的多边形列表没有特定的顺序,所以第一个多边形可能属于第 5 组,而它的邻居属于第 1 组,依此类推。
因此,我首先按组编号对列表进行排序,以使属于特定组的所有多边形彼此相邻。
然后我遍历排序列表并将每个多边形附加到一个临时列表。在达到新的组类型后,我知道是时候将临时列表中的所有多边形添加到它们自己的 PatchCollection 中了。
下面是这个逻辑的一些代码:
a = [x for x in original_groups] # The original group numbers (unsorted)
idx = sorted(range(len(a)), key=lambda k: a[k]) # Get indices of sorted group numbers
current_group = original_groups[idx[0]] # Set the current group to the be the first sorted group number
temp_patches = [] # Create a temporary patch list
for i in idx: # iterate through the sorted indices
if current_group == original_groups[i]: # Detect whether a change in group number has occured
temp_patches.append(original_patches[i]) # Add patch to the temporary variable since group number didn't change
else:
p = PatchCollection(temp_patches, alpha=0.6) # Add all patches belonging to the current group number to a PatchCollection
p.set_color([random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]) # Set all shapes belonging to this group to the same random color
ax.add_collection(p) # Add all shapes belonging this group to the axes object
current_group = original_groups[i] # The group number has changed, so update the current group number
temp_patches = [original_patches[i]] # Reset temp_patches, to begin collecting patches of the next group number
p = PatchCollection(temp_patches, alpha=0.6) # temp_patches currently contains the patches belonging to the last group. Add them to a PatchCollection
p.set_color([random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)])
ax.add_collection(p)
ax.autoscale() # Default scale may not capture the appropriate region
plt.show()
Run Code Online (Sandbox Code Playgroud)
通过打开 match_original 选项,您可以单独设置多边形的颜色(例如Polygon(vertices, color=[1, 0, 0])
)
PatchCollection(patches, match_original=True)
Run Code Online (Sandbox Code Playgroud)