Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib/seaborn: first and last row cut in half of heatmap plot

When plotting heatmaps with seaborn (and correlation matrices with matplotlib) the first and the last row is cut in halve. This happens also when I run this minimal code example which I found online.

import pandas as pd import seaborn as sns import matplotlib.pyplot as plt  data = pd.read_csv('https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv') plt.figure(figsize=(10,5)) sns.heatmap(data.corr()) plt.show() 

And get this result (I am not allowed to embed images yet) The labels at the y axis are on the correct spot, but the rows aren't completely there.

A few days ago, it work as intended. Since then, I installed texlive-xetex so I removed it again but it didn't solve my problem.

Any ideas what I could be missing?

like image 735
Flops Avatar asked Jul 08 '19 21:07

Flops


People also ask

What is Seaborn correlation heatmap?

Correlation Heatmap Pandas / Seaborn Code Example Method corr() is invoked on the Pandas DataFrame to determine the correlation between different variables including predictor and response variables. The Seaborn heatmap() method is used to create the heat map representing the correlation matrix.

How do you add a title to Seaborn heatmap?

Use the set_title() Function to Add a Title to a Seaborn Plot. A seaborn plot returns a matplotlib axes instance type object. For such objects, we can use the set_title() function to add a title to the plot. We can also control the size of the title using the fontsize parameter.


2 Answers

Unfortunately matplotlib 3.1.1 broke seaborn heatmaps; and in general inverted axes with fixed ticks.
This is fixed in the current development version; you may hence

  • revert to matplotlib 3.1.0
  • use matplotlib 3.1.2 or higher
  • set the heatmap limits manually (ax.set_ylim(bottom, top) # set the ylim to bottom, top)
like image 149
ImportanceOfBeingErnest Avatar answered Sep 25 '22 11:09

ImportanceOfBeingErnest


Its a bug in the matplotlib regression between 3.1.0 and 3.1.1 You can correct this by:

import seaborn as sns df_corr = someDataFrame.corr() ax = sns.heatmap(df_corr, annot=True) #notation: "annot" not "annote" bottom, top = ax.get_ylim() ax.set_ylim(bottom + 0.5, top - 0.5) 
like image 34
Nikhil Pakki Avatar answered Sep 25 '22 11:09

Nikhil Pakki