Missing labels in Matplotlib correlation heatmap

懵懂的女人 提交于 2020-04-07 03:28:49

问题


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!


回答1:


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:



来源:https://stackoverflow.com/questions/49122308/missing-labels-in-matplotlib-correlation-heatmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!