简体   繁体   English

如何用Matploblib用户定义的颜色图找到白色?

[英]How to find the color white with Matploblib user-defined colormap?

I create a bunch of RegularPolygons with this constructor - 我使用此构造函数创建了一堆RegularPolygons-

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

As you can see, I want the edges of these patches to be white. 如您所见,我希望这些补丁的边缘为白色。 I put them all into a list called brushShapes and then create a PatchCollection - 我将它们全部放入一个名为brushShapes的列表中,然后创建一个PatchCollection-

self.brushShapesPC = PatchCollection(self.brushShapes, match_original=True)

This way works just fine with keeping the edges white. 这样可以使边缘保持白色,效果很好。 However, now I want to use a user-defined colormap - 但是,现在我想使用用户定义的颜色图-

colormap = {'red':  ((0.0, 0.0, 0.0),
               (0.25,0.0, 0.0),
               (0.5, 0.8, 1.0),
               (0.75,1.0, 1.0),
               (1.0, 0.4, 1.0)),

        'green': ((0.0, 0.0, 0.0),
               (0.25,0.0, 0.0),
               (0.5, 0.9, 0.9),
               (0.75,0.0, 0.0),
               (1.0, 0.0, 0.0)),

        'blue':  ((0.0, 0.0, 0.4),
               (0.25,1.0, 1.0),
               (0.5, 1.0, 0.8),
               (0.75,0.0, 0.0),
               (1.0, 0.0, 0.0))} 

So now my PatchCollection instantiation is - 所以现在我的PatchCollection实例是-

self.brushShapesPC = PatchCollection(self.brushShapes, cmap=mpl.colors.LinearSegmentedColormap('SOMcolormap', self.colormap))

But now the edges have the same color as the face! 但是现在边缘与脸部的颜色相同! So what I need to do is - determine what the value would be for white with the new colormap..and change the 所以我需要做的是-用新的颜色图确定白色的值是什么,然后更改

edgecolor = (1,1,1)

to

edgecolor = (whatever_white_is)

in this constructor - 在此构造函数中-

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

is that right? 那正确吗? I have gotten very stuck trying to determine what the value for white would be. 我非常想确定白色的价值。 When I bring up a colorbar, it shows white being right in the center...so I've tried (0.5, 0.5, 0.5), (0,1,0), etc. Can anyone help me figure out what I should put? 调出颜色条时,它显示白色正好位于中心...因此,我尝试了(0.5、0.5、0.5),(0,1,0)等。有人可以帮我弄清楚我应该怎么做放? Is there a generic way to know what white is for any given colormap? 是否有一种通用方法可以知道给定颜色图的白色是什么?

I think you're going about this a bit backwards. 我认为您对此会有些退缩。 There's no completely generic way to determine what white would be in a colormap (it may exist more than once or not at all). 没有完全通用的方法来确定颜色图中的白色(它可能不止一次存在或根本不存在)。

However, you can just specify that the edges of the polygons should be white when using a PolyCollection while still using the colormap for the faces. 但是,您可以仅指定在使用PolyCollection时仍将多边形的边缘设为白色,而使用PolyCollection时,多边形的边缘应为白色。 Just specify edgecolors instead of edgecolor . 只需指定edgecolors而不是edgecolor It's a touch confusing, but the idea is that it's plural, as you could have multiple values specified. 这有点令人困惑,但其想法是复数形式,因为您可以指定多个值。 Also, have a look at RegularPolygonCollection 另外,看看RegularPolygonCollection

As a quick example: 作为一个简单的例子:

import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
from matplotlib.collections import PatchCollection
import numpy as np

xy = np.random.random((10,2))
z = np.random.random(10)

patches = [RegularPolygon((x,y), 5, 0.1) for x, y in xy]
collection = PatchCollection(patches, array=z, edgecolors='white', lw=2)

fig, ax = plt.subplots()
# So that the white edges show up...
ax.patch.set(facecolor='black')
ax.add_collection(collection)
ax.autoscale()

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM