Matplotlib matshow: show all tick labels

har*_*yka 2 python matplotlib pandas

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()
Run Code Online (Sandbox Code Playgroud)

The result looks like this: 在此输入图像描述

我读到这个问题:How to display all label values in matplotlib? 但那里的答案对我不起作用。如果我不明确设置刻度,或者以任何方式设置它们,则该数字不会改变。

还尝试了这个问题的解决方案:How to make matplotlib show all x paths? 是的plt.xticks(list(range(0,len(names)))),但这也没有任何作用。

Cor*_*ien 5

您可以使用MultipleLocator

from matplotlib.ticker import MultipleLocator  # <- HERE

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.yaxis.set_major_locator(MultipleLocator(1))  # <- HERE
ax.xaxis.set_major_locator(MultipleLocator(1))  # <- HERE
plt.show()
Run Code Online (Sandbox Code Playgroud)

热图