Question

I create a bunch of RegularPolygons with this constructor -

        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 -

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 -

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? Is there a generic way to know what white is for any given colormap?

Was it helpful?

Solution

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. Just specify edgecolors instead of 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

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

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top