kostas
kostas

Reputation: 111

How to get colorbar to scale from dark to bright?

Using the code below, I plot two rectangular patches with color that matches an arithmetic value. I have included a colorbar for the interpretation of the data values. How can I get the colorbar to scale as: dark blue --> white?

 import matplotlib.pyplot as plt
 import matplotlib.patches as patches
 from matplotlib import cm
 from matplotlib.colors import Normalize
 import numpy as np

 f,ax=plt.subplots()  
 blues=plt.get_cmap('Blues')

 norm = Normalize(vmin=0, vmax=2)
 sm = plt.cm.ScalarMappable(cmap=blues, norm=norm)
 sm._A = []
 ax.add_patch(patches.Rectangle((2,2),width=4.5, height=4, 
 facecolor=blues(norm(2)), alpha=1,label='a'))
 ax.add_patch(patches.Rectangle((2,-2),width=4.5, height=4, 
 facecolor=blues(norm(0.9)), alpha=1,label='b'))

 plt.colorbar(sm)
 plt.show()
 plt.xlim(xmin=-10,xmax=10)
 plt.ylim(ymin=-10,ymax=10)

 plt.show()

example output

Upvotes: 2

Views: 656

Answers (1)

jb326
jb326

Reputation: 1355

For any builtin color map, you can append _r to the end of its name to get a reversed colormap. So replace blues=plt.get_cmap('Blues') with blues=plt.get_cmap('Blues_r').

For reference, see the bottom of this page:

This reference example shows all colormaps included with Matplotlib. Note that any colormap listed here can be reversed by appending "_r" (e.g., "pink_r").

Upvotes: 3

Related Questions