Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add alpha to an existing matplotlib colormap

I'd like to make an overlay of several hexbin plots, but with builtin colormaps only the last one is visible. I don't want to construct a colormap de novo. How one would add linear alpha to the colormap without knowing the inner structure of the colormap beforehand?

like image 974
Hennadii Madan Avatar asked May 19 '16 15:05

Hennadii Madan


People also ask

What is Alpha in Matplotlib plot?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

What is the default colormap for Matplotlib?

Colormap. The new default colormap used by matplotlib. cm. ScalarMappable instances is 'viridis' (aka option D).

What is Matplotlib colormap?

The colormap is a dictionary which maps numbers to colors. Matplotlib provides many built-in colormaps. When you have a 2D array, such as data above, the values at each grid point is a float between 0 and 1. The gray colormap maps 0 to black and 1 to white. The jet colormap maps 0 to blue and 1 to red.


1 Answers

I'm not quite sure if this qualifies within "not knowing the inner structure of the colormap", but perhaps something like this would work to add a linear alpha to an existing colormap?

import numpy as np import matplotlib.pylab as pl from matplotlib.colors import ListedColormap  # Random data data1 = np.random.random((4,4))  # Choose colormap cmap = pl.cm.RdBu  # Get the colormap colors my_cmap = cmap(np.arange(cmap.N))  # Set alpha my_cmap[:,-1] = np.linspace(0, 1, cmap.N)  # Create new colormap my_cmap = ListedColormap(my_cmap)  pl.figure() pl.subplot(121) pl.pcolormesh(data1, cmap=pl.cm.RdBu) pl.colorbar()  pl.subplot(122) pl.pcolormesh(data1, cmap=my_cmap) pl.colorbar() 

enter image description here

like image 192
Bart Avatar answered Oct 10 '22 08:10

Bart