views:

75

answers:

1

Hi all,

I start using matplotlib a month ago, so I'm still learning.
I'm trying to do a heatmap with matshow. My code is the following:

data = numpy.array(a).reshape(4, 4)  
cax = ax.matshow(data, interpolation='nearest', cmap=cm.get_cmap('PuBu'), norm=LogNorm())  
cbar = fig.colorbar(cax)

ax.set_xticklabels(alpha)  
ax.set_yticklabels(alpha)

where alpha is a model from django with 4fields: 'ABC', 'DEF', 'GHI', 'JKL'

the thing is that I don't know why, the label 'ABC' doesn't appear, leaving the last cell without label.
If someone would have a clue how to modify my script in a way to appear the 'ABC' I would be grateful :)

A: 

What's happening is that the xticks actually extend outside of the displayed figure when using matshow. (I'm not quite sure exactly why this is. I've almost never used matshow, though.)

To demonstrate this, look at the output of ax.get_xticks(). In your case, it's array([-1., 0., 1., 2., 3., 4.]). Therefore, when you set the xtick labels, "ABC" is at <-1, -1>, and isn't displayed on the figure.

The easiest solution is just to prepend a blank label to your list of labels, e.g.

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

As a full example:

import numpy as np
import matplotlib.pyplot as plt

alpha = ['ABC', 'DEF', 'GHI', 'JKL']

data = np.random.random((4,4))

fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(data, interpolation='nearest')
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

plt.show()

Matshow example

Joe Kington
Thank you! I've been able to see the same thing :)But I've another problem..I'm doing two heatmaps, one (4x4) and another(9x9). The program label the heatmap 0,2,4,6,8.. So when I tried to label, it only gives a name to those numbers.I've tried with ax.set_xticks(matplotlib.numpy.arange(len(list_names))) but it decrease the size of cells..
Pat
@Patricia - If you set the tick locations manually, a call to `ax.axis('image')` after you set them should fix the problem you described. Hope that helps!
Joe Kington
Thanks! Problem solved :)So ax.axis('image') restore the original size of the image, right?
Pat
Basically. (I'm honestly not sure why the axis limits change when the tick locations are set... This might be a bug...) See here for more information on what `ax.axis('image')` does: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis
Joe Kington