colormap from blue to transparent to red

Hi everyone,

I am having trouble with colormaps unsing pcolormesh. I would like to
plot and colorise a seismic wave field above a map. Plotting works fine
but I do not know how to bring transparency into colormaps. For negative
values I want the coloration being blue then it should become
transparent and the greatest value should be drawn red. I have tried a
lot but without any success. As far as I can see, the keyarg alpha does
not fit my needs at all.

Do you have any suggestions for me?

Regards

Stefan

Hi everyone,

I am having trouble with colormaps unsing pcolormesh. I would like to

plot and colorise a seismic wave field above a map. Plotting works fine

but I do not know how to bring transparency into colormaps. For negative

values I want the coloration being blue then it should become

transparent and the greatest value should be drawn red. I have tried a

lot but without any success. As far as I can see, the keyarg alpha does

not fit my needs at all.

Do you have any suggestions for me?

Regards

Stefan

Stefan,

If you mean using the keyword ‘alpha’ in the pcolormesh function, then yes, that isn’t the right place for it. In your case, the right place to specify the alpha channel is in the colormap itself. Unfortunately, it is currently designed around the idea of a scalar value specifying the transparency.

What you can do is a little trick. First, get the colormap object and then initialize it. This will cause it to internally create an array called “_lut” which holds rgba values.

theCM = cm.get_cmap(‘somemap’)

theCM._init()

Then, you need to fill in the alpha values for yourself. For this, I am going to use a triangle function:

alphas = np.abs(np.linspace(-1.0, 1.0, theCM.N))
theCM._lut[:,-1] = alphas

Now that your colormap is set up, you can pass it into your pcolormesh call.

plt.pcolormesh(X, Y, C, cmap=theCM)

That should work, but I have not fully tested it. I hope that helps!
Ben Root

···

On Tue, Oct 12, 2010 at 7:57 AM, Stefan Mauerberger <stefan.mauerberger@…3317…> wrote:

You can't make the actual colormap contain transparent color entries,
but you can easily plot a masked array using a custom colormap. The
attached script based on
http://matplotlib.sourceforge.net/examples/pylab_examples/custom_cmap.html
should help you get what you want.

The important part of the script is to create a Numpy masked array to
exclude the regions you'd like to appear transparent:

cond = (-0.1 < Z) & (Z < 0.1)
Z_masked = np.ma.masked_where(cond, Z)

Cheers,
Scott

transparent-colorbar-workaround.py (1.05 KB)

···

On 12 October 2010 14:57, Stefan Mauerberger <stefan.mauerberger@...3317...> wrote:

I am having trouble with colormaps unsing pcolormesh. I would like to
plot and colorise a seismic wave field above a map. Plotting works fine
but I do not know how to bring transparency into colormaps. For negative
values I want the coloration being blue then it should become
transparent and the greatest value should be drawn red. I have tried a
lot but without any success. As far as I can see, the keyarg alpha does
not fit my needs at all.

Do you have any suggestions for me?

Hi everyone,

I am having trouble with colormaps unsing pcolormesh. I would like to

plot and colorise a seismic wave field above a map. Plotting works fine

but I do not know how to bring transparency into colormaps. For negative

values I want the coloration being blue then it should become

transparent and the greatest value should be drawn red. I have tried a

lot but without any success. As far as I can see, the keyarg alpha does

not fit my needs at all.

Do you have any suggestions for me?

Regards

Stefan

Stefan,

If you mean using the keyword ‘alpha’ in the pcolormesh function, then yes, that isn’t the right place for it. In your case, the right place to specify the alpha channel is in the colormap itself. Unfortunately, it is currently designed around the idea of a scalar value specifying the transparency.

What you can do is a little trick. First, get the colormap object and then initialize it. This will cause it to internally create an array called “_lut” which holds rgba values.

theCM = cm.get_cmap(‘somemap’)

theCM._init()

Then, you need to fill in the alpha values for yourself. For this, I am going to use a triangle function:

alphas = np.abs(np.linspace(-1.0, 1.0, theCM.N))
theCM._lut[:,-1] = alphas

Now that your colormap is set up, you can pass it into your pcolormesh call.

plt.pcolormesh(X, Y, C, cmap=theCM)

That should work, but I have not fully tested it. I hope that helps!
Ben Root

Slight correction to my code example:

theCM._lut[:-3,-1] = alphas

Sorry for any confusion.

Ben Root

···

On Tue, Oct 12, 2010 at 9:25 AM, Benjamin Root <ben.root@…3146…4…> wrote:

On Tue, Oct 12, 2010 at 7:57 AM, Stefan Mauerberger <stefan.mauerberger@…3317…> wrote:

Ben, thanks a lot! Your way does exactly what I want.

Scott, I do not understand your solution, unfortunately. I have already
known the example from the gallery. But up to now, I have not dealt with
masked arrays. Anyway ...

Thanks a lot again.

Stefan