Reputation: 2851
I have a numpy matrix fooarray
of size 138 x 138. Each entry in row and column is a word. The following code is used to generate heatmap of the same matrix. But I am not able to show all the words in the plot.
It seems the values shown are also wrong in the color scale. While the values in matrix range from 3.2 to -0.2, the values shown in the heat map are from 0.1 to -0.1. How do I plot a heatmap with a numpy matrix?
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(fooarray, interpolation='nearest', cmap='hot')
fig.colorbar(cax)
ax.set_xticklabels([' | '] + labels)
ax.set_yticklabels(['|'] + labels)
plt.show()
Upvotes: 2
Views: 815
Reputation: 2491
I have not clear why you are adding [' | '] and ['|'] in front of the labels, so I removed it from the code. The color scale works for me (see code), I believe there is something wrong with your data.
The code below control ticks positions with set_xticks
and labels with ax.set_xticklabels
. I added a 90 degree rotation, but still hard to have readable labels with 138 ticks.
import numpy as np
import matplotlib.pyplot as plt
#create test data:
s=138 #size of array
labels=[str(a) for a in range(s)]
fooarray=np.random.random(s*s).reshape((s,s))
#--- original code here:
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(fooarray, interpolation='nearest', cmap='hot')
fig.colorbar(cax)
#----
#ticks and labels:
ax.set_xticks(range(len(labels)) , minor=False)
ax.set_xticklabels(labels)
ax.set_yticks(range(len(labels)) , minor=False)
ax.set_yticklabels(labels)
plt.xticks(rotation=90)
plt.show()
Upvotes: 3