On 12 October 2010 14:57, Stefan Mauerberger
<stefan.mauerber...@mnet-online.de> 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?

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
#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

cdict = {'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))
        }

blue_red = LinearSegmentedColormap('BlueRed', cdict)

x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2*np.pi, 0.1)
X, Y = np.meshgrid(x,y)
Z = np.cos(X) * np.sin(Y)

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

plt.subplot(111)
## plt.imshow(Z_masked, cmap=blue_red, interpolation='nearest')
plt.pcolormesh(X, Y, Z_masked, cmap=blue_red)
plt.colorbar()

plt.show()
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to