设置多边形颜色 Matplotlib

9
我有一个包含10,000多个Matplotlib多边形对象的列表。每个多边形属于20个组中的1个。我想通过将每个唯一组映射到唯一颜色来区分多边形所属的组。
这里有一些与我的问题类似的帖子: 使用python matplotlib为多边形填充多种颜色 如何在Matplotlib中根据变量填充多边形颜色? 如何在Matplotlib中为矩形设置颜色? 这些解决方案只是为列表中的每个形状应用随机颜色。这不是我要找的。对我而言,属于特定组的每个形状应该具有相同的颜色。有什么建议吗?
示例代码:
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()

请注意,如果您运行此代码,它将无法工作,因为poly_vertices和poly_colors尚未定义。现在,只需假设poly_vertices是多边形顶点的列表,而poly_colors是RGB颜色的列表,并且每个列表都有10000个条目。
例如:poly_vertices [0] = [(0, 0),(1,0),(0,1)],colors [0] = [1,0,0]
谢谢!
2个回答

8

好的,我知道我想要做什么了。我会为任何可能遇到类似问题的人发布答案。

由于某些原因,在多边形中设置颜色不起作用。

Polygon(vertices, color=[1, 0, 0])

无法工作。

相反,将所有多边形添加到集合中后,请使用

p = PatchCollection(patches)
p.set_color([1, 0, 0])

但是我仍然想按颜色分组多边形。因此,我需要添加多个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()

7

通过打开match_original选项,您可以单独设置多边形的颜色(例如Polygon(vertices, color=[1, 0, 0])

PatchCollection(patches, match_original=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接