Creating a Colormap Legend in Matplotlib

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') 

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.


usb device identification


How to set cookies with redirect in Pylons

Pypcap for mac on python 2.6?

Python: Inheritance of a class attribute (list)


How to increment variable names/Is this a bad idea
As usual, I figure it out right after I ask it ;).

pyopengl: Could it replace c++?
For posterity, here's my stab at it:.
Read more numbers in a line in Python
m = np.zeros((1,20)) for i in range(20):     m[0,i] = (i*5)/100.0 print m plt.imshow(m, cmap='gray', aspect=2) plt.yticks(np.arange(0)) plt.xticks(np.arange(0,25,5), [0,25,50,75,100]) 
I'm sure there exists a more elegant solution.. Vince.

88 out of 100 based on 88 user ratings 238 reviews