1

I want to plot a probability distribution over a map using tripcolor and want the distribution to fade to transparency where the probability is low/zero. However tripcolor doesn't seem to accept local alpha-values provided by the colormap.

I set up a custom colormap that transitions from a transparent (alpha=0.) white to some blueish color (alpha=1.), as described in the matplotlib docs.

cdict = {'red': ((0., 1., 1.), (1., 0., 0.)), 'green': ((0., 1., 1.), (1., 0.5, 0.5)), 'blue': ((0., 1., 1.), (1., 1., 1.)), 'alpha': ((0., 0., 0.), (1., 1., 1.))} testcmap = colors.LinearSegmentedColormap('test', cdict) plt.register_cmap(cmap=testcmap) 

If I apply this to a line, as described here everything works fine.

However if I want to use tripcolor to draw the distribution, it seems to ignore the colormap alpha values...

It works for a scatter plot.

A minimal working example can be found below.

import numpy as np from matplotlib import pyplot as plt, colors, cm # quick and dirty test data ext = np.linspace(0., 1., 21) coords, _ = np.meshgrid(ext, ext) x = coords.flatten() y = coords.T.flatten() vals = 1. - np.sin(coords * np.pi / 2).flatten() # color dict cdict = {'red': ((0., 1., 1.), (1., 0., 0.)), 'green': ((0., 1., 1.), (1., 0.5, 0.5)), 'blue': ((0., 1., 1.), (1., 1., 1.)), 'alpha': ((0., 0., 0.), (1., 1., 1.))} # colormap from dict testcmap = colors.LinearSegmentedColormap('test', cdict) plt.register_cmap(cmap=testcmap) # plotting fig, ax = plt.subplots(1) ax.set_facecolor('black') ax.tripcolor(x, y, vals, cmap='test') fig2, ax2 = plt.subplots(1) ax2.set_facecolor('black') ax2.scatter(x, y, c=vals, cmap='test') plt.show() 

Edit: Looking at the sourcecode line 118 seems to set a global alpha for the triangulation. Copy/pasting the tripcolor function and omitting this line worked. However it would still be nice to use matplotlibs built-in functions...

Edit2: Changed the data generation function from cos to 1-sin to get a more suggestive transition. For the first edit to give a nice result I also hat to use shading='gouraud'.

4
  • Hmmm... when increasing ext = np.linspace(0., 1., 21) to ext = np.linspace(0., 1., 200) tripcolor does not look bad at all... Commented Sep 17, 2019 at 12:34
  • @Sosel tripcolor still doesn't show any transparency to me. The scatter plot appear more tripcolor-like, yeah, but using scatters as a workaround is not what I want... Commented Sep 17, 2019 at 12:40
  • With fine granularity both, scatter and tripcolor nearly look identical here, so probably I just do not fully understand... Commented Sep 17, 2019 at 12:49
  • @Sosel Can you look at the right edge in the plots? For me there is a sharp jump-like boundary between white and black in the tripcolor plot, while the scatter plot fades to black smoothly. This is rather different, even if I increase the granularity to 200... Edit: It appears the cosine was not the best choice to indicate the behaviour I want, see the updated question. Commented Sep 17, 2019 at 12:54

1 Answer 1

2

Is this closer to what you are looking for?

ax.tripcolor(x, y, vals, cmap='test', alpha=None) 

Not sure why, but my guess is that setting alpha=None allows each triangle to get the alpha color from the colormap.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, for most other artists alpha is already None, but for the tripcolor it defaults to 1.0 so one will need to set it to None manually.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.