gpt4 book ai didi

python - Matplotlib matshow : show all tick labels

转载 作者:行者123 更新时间:2023-12-02 18:31:58 27 4
gpt4 key购买 nike

我是 Matplotlib 的新手,我使用 matshow 函数创建了一些数字相关性的热图。目前下面的代码只显示每 5 个标签(或刻度),我希望它显示所有标签。代码:

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()

结果是这样的: enter image description here

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

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

最佳答案

您可以使用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()

heatmap

关于python - Matplotlib matshow : show all tick labels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69305632/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com