Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Colormap Legend in Matplotlib

I am using imshow() in matplotlib like so:

import numpy as np import matplotlib.pyplot as plt mat = '''SOME MATRIX''' plt.imshow(mat, origin="lower", cmap='gray', interpolation='nearest') plt.show() 

How do I add a legend showing the numeric value for the different shades of gray. Sadly, my googling has not uncovered an answer :(

Thank you in advance for the help.

Vince

like image 228
Vince Avatar asked Mar 15 '10 23:03

Vince


1 Answers

Simple, just plt.colorbar():

import numpy as np import matplotlib.pyplot as plt mat = np.random.random((10,10)) plt.imshow(mat, origin="lower", cmap='gray', interpolation='nearest') plt.colorbar() plt.show() 
like image 148
wordsforthewise Avatar answered Oct 05 '22 17:10

wordsforthewise