本文介绍了Matplotlib相关热图中缺少标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UCI机器学习存储库中的


I'm playing around with the abalone dataset from UCI's machine learning repository. I want to display a correlation heatmap using matplotlib and imshow.

The first time I tried it, it worked fine. All the numeric variables plotted and labeled, seen here:

fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(111)
plt.imshow(df.corr(), cmap='hot', interpolation='nearest')
plt.colorbar()
labels = df.columns.tolist()
ax1.set_xticklabels(labels,rotation=90, fontsize=10)
ax1.set_yticklabels(labels,fontsize=10)
plt.show()

successful heatmap

Later, I used get_dummies() on my categorical variable, like so:

df = pd.get_dummies(df, columns = ['sex'])

resulting correlation matrix

So, if I reuse the code from before to generate a nice heatmap, it should be fine, right? Wrong!

What dumpster fire is this?

So my question is, where did my labels go, and how do I get them back?!

Thanks!

解决方案

To get your labels back, you can force matplotlib to use enough xticks so that all your labels can be shown. This can be done by adding

ax1.set_xticks(np.arange(len(labels)))
ax1.set_yticks(np.arange(len(labels)))

before your statements ax1.set_xticklabels(labels,rotation=90, fontsize=10) and ax1.set_yticklabels(labels,fontsize=10).

This results in the following plot:

这篇关于Matplotlib相关热图中缺少标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 19:13