Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a section of a colormap

I really like the "RdBu_r" colormap, but I want to cutout the white part between the blues and reds. Is there an easy way to do this?

like image 540
DilithiumMatrix Avatar asked Feb 29 '16 22:02

DilithiumMatrix


People also ask

How do I get rid of Colorbar?

If you want the colorbar to be removed from plot and disappear, you have to use the method remove of the colorbar instance and to do this you need to have the colorbar in a variable, for which you have two options: holding the colorbar in a value at the moment of creation, as shown in other answers e.g. cb=plt.

What is a sequential colormap?

Sequential colormaps (that are perceptually uniform of course) are basic colormaps that start at a reasonably low lightness value and uniformly increase to a higher value. They are commonly used to represent information that is ordered.

What is CMAP in PLT Imshow?

cmap : This parameter is a colormap instance or registered colormap name. norm : This parameter is the Normalize instance scales the data values to the canonical colormap range [0, 1] for mapping to colors. vmin, vmax : These parameter are optional in nature and they are colorbar range.


1 Answers

Yes, but in your case, it's probably easier to make a colormap that interpolates between blue and red instead.

For example:

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

cmap = LinearSegmentedColormap.from_list('name', ['red', 'blue'])

fig, ax = plt.subplots()
im = ax.imshow(np.random.random((10, 10)), cmap=cmap)
fig.colorbar(im)
plt.show()

enter image description here

Note that you could substitute the exact RGB values if you wanted a shade of red that isn't an HTML color name.

However, if you did want to "cut out the middle" of another colormap, you'd evaluate it on a range that didn't include the middle and create a new colormap:

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

# Remove the middle 40% of the RdBu_r colormap
interval = np.hstack([np.linspace(0, 0.3), np.linspace(0.7, 1)])
colors = plt.cm.RdBu_r(interval)
cmap = LinearSegmentedColormap.from_list('name', colors)

# Plot a comparison of the two colormaps
fig, axes = plt.subplots(ncols=2)
data = np.random.random((10, 10))

im = axes[0].imshow(data, cmap=plt.cm.RdBu_r, vmin=0, vmax=1)
fig.colorbar(im, ax=axes[0], orientation='horizontal', ticks=[0, 0.5, 1])
axes[0].set(title='Original Colormap')

im = axes[1].imshow(data, cmap=cmap, vmin=0, vmax=1)
fig.colorbar(im, ax=axes[1], orientation='horizontal', ticks=[0, 0.5, 1])
axes[1].set(title='New Colormap')

plt.show()

enter image description here

like image 141
Joe Kington Avatar answered Oct 09 '22 14:10

Joe Kington