为什么matplotlib.PatchCollection会影响补丁的颜色?

18 投票
1 回答
5463 浏览
提问于 2025-04-17 13:28

我做了一些补丁,像这样 -

node.shape = RegularPolygon((node.posX, node.posY),
                            6,
                radius = node.radius,
                                    edgecolor = 'none',
                                    facecolor = node.fillColor,
                                    zorder = node.zorder)

node.brushShape = RegularPolygon((node.posX, node.posY),
                            6,
                node.radius * 0.8,
                linewidth = 3,
                                    edgecolor = (1,1,1),
                                    facecolor = 'none',
                                    zorder = node.zorder)

最开始我只是把它们直接放到我的坐标轴上,像这样 -

self.plotAxes.add_artist(node.shape)
self.plotAxes.add_artist(node.brushShape)

这样做是没问题的。但是现在我想把这些补丁放进一个补丁集合(PatchCollection)里,然后再把这个集合放到坐标轴上。然而,当我这么做的时候,我所有的形状都变成蓝色了。我不明白为什么把它放进集合里就会改变颜色。有没有人能帮我,告诉我怎么做才能保持我输入的颜色值作为补丁的面颜色(faceColor)?

新的代码是 -

node.shape = RegularPolygon((node.posX, node.posY),
                        6,
            radius = node.radius,
                                edgecolor = 'none',
                                facecolor = node.fillColor,
                                zorder = node.zorder)

node.brushShape = RegularPolygon((node.posX, node.posY),
                        6,
            node.radius * 0.8,
            linewidth = 3,
                                edgecolor = (1,1,1),
                                facecolor = 'none',
                                zorder = node.zorder)

self.patches.append(node.shape)
self.patches.append(node.brushShape)


self.p = PatchCollection(self.patches) 
self.plotAxes.add_collection(self.p) 

1 个回答

27
self.p = PatchCollection(self.patches, match_original=True) 

默认情况下,补丁集合会覆盖给定的颜色(文档),这样做是为了能够应用颜色映射、循环颜色等等。这是一个集合级别的功能(也是散点图背后代码的动力)。

撰写回答