设置多边形颜色Matplotlib [英] Set Polygon Colors Matplotlib

查看:174
本文介绍了设置多边形颜色Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of 10,000+ Matplotlib Polygon objects. Each polygon belongs to 1 of 20 groups. I want to differentiate which group a polygon belongs to by mapping each unique group to a unique color.

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

Here are some posts I've found with similar issues to mine:

在python matplotlib中填充具有多种颜色的多边形

如何使用基于Matplotlib中的变量显示颜色?

如何在Matplotlib中将颜色设置为Rectangle?

这些解决方案仅将随机颜色应用于列表中的每个形状.那不是我想要的.对我来说,属于特定组的每个形状应具有相同的颜色.有什么想法吗?

These solutions simply apply a random color to each shape in the list. That is not what I am looking for. For me, each shape belonging to a particular group should have the same color. Any ideas?

示例代码:

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个条目.

Note that if you run this code, it won't work because poly_vertices and poly_colors haven't been defined. For now, just assume that poly_vertices is a list of polygon vertices, and poly_colors is a list of RGB colors and each list has 10000 entries.

例如:poly_vertices [0] = [(0,0),(1,0),(0,1)],颜色[0] = [1,0,0]

For example: poly_vertices[0] = [(0, 0), (1, 0), (0, 1)], colors[0] = [1, 0, 0]

谢谢!

推荐答案

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

Okay, I figured out what I was trying to do. I'll post the answer for anyone who may be having similar issues.

由于某种原因,无法在多边形本身中设置颜色.即

For some reason, setting the color in the polygon itself doesn't work. i.e.

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

不起作用.

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

Instead, after adding all the polygons to a collection, use

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

但是我仍然想按颜色对多边形进行分组.因此,我需要添加多个PatchCollections-每个组类型一个!

But I still want to group polygons by color. Therefore I need to add multiple PatchCollections -- one for each group type!

我最初的多边形列表没有特定的顺序,因此第一个多边形可能属于第5组,而其邻居则属于第1组,依此类推.

My original list of polygons were in no particular order, so the first polygon may belong to group 5, while its neighbor belongs to group 1, etc.

因此,我首先按组号对列表进行排序,以使属于特定组的所有多边形彼此相邻.

So, I first sorted the list by group number such that all polygons belonging to a particular group were right next to each other.

然后我遍历排序列表并将每个多边形附加到临时列表.达到新的组类型后,我知道是时候将临时列表中的所有多边形添加到它们自己的PatchCollection中了.

I then iterated through the sorted list and appended each polygon to a temporary list. Upon reaching a new group type, I knew it was time to add all the polygons in the temporary list to their own PatchCollection.

以下是此逻辑的一些代码:

Here's some code for this logic:

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

这篇关于设置多边形颜色Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆