设置多边形颜色Matplotlib

时间:2017-06-23 15:34:01

标签: python matplotlib colors polygon colormap

我有10,000多个Matplotlib Polygon对象的列表。每个多边形属于20个组中的1个。我想通过将每个唯一的组映射到唯一的颜色来区分多边形所属的组。

以下是我发现的类似问题的一些帖子:

Fill polygons with multiple colors in python matplotlib

How to fill polygons with colors based on a variable in Matplotlib?

How do I set color to Rectangle in 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 个答案:

答案 0 :(得分:3)

好的,我想出了我想要做的事情。我会为可能遇到类似问题的人发布答案。

出于某种原因,在多边形中设置颜色本身并不起作用。即。

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()

答案 1 :(得分:3)

通过启用match_original选项,您可以分别设置多边形的颜色(例如Polygon(vertices, color=[1, 0, 0])

PatchCollection(patches, match_original=True)