Seaborn: Seaborn Heatmap - yticks off center by default

Created on 30 Aug 2019  路  3Comments  路  Source: mwaskom/seaborn

Greetings, yticks, yticklabels, and annotations seem to be off center on the yaxis for some reason (by default). Is this user error, and if so I am having trouble googling a solution and any help is appreciated!

Version info:
seaborn-0.9.0
scipy-1.3.0
scikit_learn-0.21.3
pandas-0.25.0
numpy-1.17.0
matplotlib-3.1.1

Code:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = {'y_Predicted': [1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0], 'y_Actual':    [1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0]}

df = pd.DataFrame(data, columns=['y_Actual','y_Predicted'])
confusion_matrix = pd.crosstab(df['y_Actual'], df['y_Predicted'], rownames=['Actual'], colnames=['Predicted'])
fig = plt.figure(figsize=(12,8))
sns.heatmap(confusion_matrix, annot=True)
plt.show()

Observed phenotype from code above:
image

Thanks!
-Sungshine

Most helpful comment

This is a bug in matplotlib and a duplicate of several issues that will come up if you search. I believe there is a now a newer release of matplotlib that fixes it.

All 3 comments

This is a bug in matplotlib and a duplicate of several issues that will come up if you search. I believe there is a now a newer release of matplotlib that fixes it.

Thanks for the swift response!
Installing the most recent development version of matplotlib (vs. current release version 3.1.1) seems to fix the issue. It will require cloning the repo from git and installing from source, clear instructions can be found here: https://matplotlib.org/devdocs/users/installing.html#installing-from-source

You can also fix the alignment problem with one line of code:

plt.setp(plt.gca().yaxis.get_majorticklabels(), va = "center")
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wenhaosun picture wenhaosun  路  3Comments

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

tritemio picture tritemio  路  3Comments

TDaltonC picture TDaltonC  路  3Comments

btyukodi picture btyukodi  路  3Comments