How to add legend to imshow() in matplotlib
Posted
by
rankthefirst
on Stack Overflow
See other posts from Stack Overflow
or by rankthefirst
Published on 2014-08-25T09:32:03Z
Indexed on
2014/08/25
10:20 UTC
Read the original article
Hit count: 427
I am using matplotlib
In plot()
or bar()
, we can easily put legend, if we add labels to them. but what if it is a contourf()
or imshow()
I know there is a colorbar()
which can present the color range, but it is not satisfied. I want such a legend which have names(labels).
For what I can think of is that, add labels to each element in the matrix, then ,try legend(), to see if it works, but how to add label to the element, like a value??
in my case, the raw data is like:
1,2,3,3,4
2,3,4,4,5
1,1,1,2,2
for example, 1 represents 'grass', 2 represents 'sand', 3 represents 'hill'... and so on. imshow() works perfectly with my case, but without the legend.
my question is:
Is there a function that can automatically add legend, for example, in my case, I just have to do like this: someFunction('grass','sand',...)
If there isn't, how do I add labels to each value in the matrix. For example, label all the 1 in the matrix 'grass', labell all the 2 in the matrix 'sand'...and so on.
Thank you!
© Stack Overflow or respective owner