How to align xlabels and ylabels in seaborn? [dupl

2019-08-05 16:00发布

问题:

This question already has an answer here:

  • Changing the rotation of tick labels in Seaborn heatmap 2 answers

I am plotting heatmap of features but the feature names on x and y axes conincide with each other. So how can I align x axis feature names vertically and y axis feature names horizontally so that they do not overlap.

Code:

%matplotlib notebook
corr = data.loc[:,'PERID':'PRXRETRY'].corr()

sns.set(font_scale=0.8)
sns.heatmap(corr, 
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values,cmap="YlGnBu",annot=True)

Screenshot:

回答1:

This works for me:

hm = sns.heatmap(corr, cmap="YlGnBu", annot=True)
hm.set_xticklabels(labels=corr.columns.values, rotation=90)
hm.set_yticklabels(labels=corr.columns.values, rotation=0)