19

I am having some difficulty with pyplot's awesome drawing abilities. I have selected my very own colormap

n = 6
map = matplotlib.cm.get_cmap('Dark2')
cmap = colors.ListedColormap([(0,0,0,0)] + [[map(i * 1.0 / n)[j] for j in range(3)] + [0.2] for i in range(1, n + 1)])

This is basically just the Dark2 colormap, discretized to n (in my case 6) values with the zero value mapping to pure white. The main difference, however, is that the alpha values for my custom colormap are set to 0.2, not 1 as is default.

The problem is that when I plot something using this, like

plt.pcolormesh(np.random.rand(10,10), cmap = cmapInv)

the result is something like this:

Result

This looks nice enough, but you can clearly see that around each box, there is a very thin border of the same color as the box but with alpha set to 1.

EDIT: As suggested in the comments, the cause of these borders is probably overlap between the boxes.

Is there a way to clean this up?

5xum
  • 5,250
  • 8
  • 36
  • 56
  • 2
    Could it be that neighboring boxes just overlap slightly? I don't think there is a border around each box. – David Zwicker Dec 19 '13 at 10:50
  • That sounds very likely. Any idea how to work around this? – 5xum Dec 19 '13 at 10:54
  • You could try different backends or different output formats. How do you create the PNG in detail? – David Zwicker Dec 19 '13 at 11:00
  • I just saved the output that the method put out (in the window it creates) – 5xum Dec 19 '13 at 12:59
  • I'm afraid that you have to save the image and then write an image-processing script to erase the overlaps. – Timothy Dec 19 '13 at 20:18
  • 1
    This may be related: https://github.com/matplotlib/matplotlib/issues/1188 – tacaswell Dec 20 '13 at 06:01
  • I remember seeing something else that `pcolormesh` does not play nicely with alpha, but can not track it down. I think this is worth opening as an issue on github (with this lovely example). – tacaswell Dec 20 '13 at 06:02
  • if this is different that the 1188, if it is the same please add this example to that thread. – tacaswell Dec 20 '13 at 06:07
  • I see. As far as I understand, this is obviously a minor bug in pyplot, so there's nothing more to be done here. If you just post your explanation as an answer, I will gladly accept it. – 5xum Dec 20 '13 at 07:55

1 Answers1

7

As a minor workaround in the meantime, I found you can get the image closer to what you want by messing with the edgecolor and linewidth attributes. For example, using the following input to pcolormesh:

    plt.pcolormesh(np.random.rand(10,10), cmap = cmapInv, edgecolor=(1.0, 1.0, 1.0, 0.3), linewidth=0.0015625)

outputs the following image:

enter image description here

  • 2
    Why this particular choice of values edgecolor=(1.0, 1.0, 1.0, 0.3), linewidth=0.0015625 ? – Andreas K. Jan 24 '18 at 11:44
  • 1
    I settled on the values after trial and error. The idea is to use edges to lighten the overlaps between the squares, so I needed white with the appropriate level of transparency to make overlaps match the interiors, and the line width was chosen to be approximately the width of the overlap. – Jonathan A. Gross Jan 25 '18 at 16:23