设置多边形颜色 Matplotlib
Set Polygon Colors Matplotlib
我有一个包含 10,000 多个 Matplotlib 多边形对象的列表。每个多边形属于 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)], 颜色[0] = [1, 0, 0]
谢谢!
好的,我知道我想做什么了。我会 post 为可能遇到类似问题的任何人提供答案。
出于某种原因,在多边形本身中设置颜色不起作用。即
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()
通过启用match_original选项,您可以单独设置多边形的颜色(例如Polygon(vertices, color=[1, 0, 0])
)
PatchCollection(patches, match_original=True)
我有一个包含 10,000 多个 Matplotlib 多边形对象的列表。每个多边形属于 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)], 颜色[0] = [1, 0, 0]
谢谢!
好的,我知道我想做什么了。我会 post 为可能遇到类似问题的任何人提供答案。
出于某种原因,在多边形本身中设置颜色不起作用。即
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()
通过启用match_original选项,您可以单独设置多边形的颜色(例如Polygon(vertices, color=[1, 0, 0])
)
PatchCollection(patches, match_original=True)