Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing labels in Matplotlib correlation heatmap

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!

like image 536
MahiMai Avatar asked Mar 06 '18 01:03

MahiMai


1 Answers

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:

enter image description here

like image 69
jdamp Avatar answered Oct 20 '22 05:10

jdamp