Home > database >  Matplotlib matshow: show all tick labels
Matplotlib matshow: show all tick labels

Time:09-24

I am new to Matplotlib and I created a Heatmap of some number's correlation using the matshow function. Currently the code below only displays every 5th label (or tick), and I want it to display all of it. The code:

names = list('ABCDEDFGHIJKLMNOPQRSTU')

#just some random data for reproductability
df = pd.DataFrame(np.random.randint(0,100,size=(100, 22)), columns=names)

fig = plt.figure()
ax = fig.add_subplot(111)

cor_matrix = df.corr()

#these two lines don't change the outcome
ax.set_xticks(np.arange(len(names)))
ax.set_yticks(list(range(0,len(names))))

ax.matshow(cor_matrix)
plt.show()

The result looks like this: enter image description here

I read this question: heatmap

CodePudding user response:

The order of the matplotlib functions is causing the issue. By calling ax.matshow(cor_matrix) after the assignment of the x- and y-ticks they are overwritten again. By changing the ordering, everything should just work fine.

New order:

names = list('ABCDEDFGHIJKLMNOPQRSTU')

#just some random data for reproductability
df = pd.DataFrame(np.random.randint(0,100,size=(100, 22)), columns=names)

fig = plt.figure()
ax = fig.add_subplot(111)

cor_matrix = df.corr()


ax.matshow(cor_matrix)

ax.set_xticks(np.arange(len(names), step=1))
ax.set_yticks(list(range(0,len(names))))

plt.show()

Output:

enter image description here

  • Related