By Flops

2019-07-08 21:18:45 8 Comments

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('')

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?


@Sujit Bhattacharyya 2020-01-26 17:27:34

conda install matplotlib=3.1.0

This worked for me and downgraded matplotlib from 3.1.1 to 3.1.0 and the heatmaps started to behave correctly

@Magnus Berg Sletfjerding 2020-01-09 13:07:12

I solved it by adding this line in my code, with matplotlib==3.1.1:

ax.set_ylim(sorted(ax.get_xlim(), reverse=True))

NB. The only reason this works is because the x-axis isn't changed, so use at your own risk with future mpl versions

@Lorenz 2019-12-07 17:41:58

rustyDev is right about conda-forge, but I did not need to do a manual pip install from a github download. For me, on Windows, it worked directly. And the plots are all nice again.

conda install -c conda-forge matplotlib

optional points, not needed for the answer:

Afterwards, I tried other steps, but they are not needed: In conda prompt: conda search matplotlib --info showed no new version info, the most recent info was for 3.1.1. Thus I tried pip using pip install matplotlib==3.1.2 But pip says "Requirement already satisfied"

Then getting the version according to… python - import matplotlib - matplotlib.__version__ shows that 3.1.2 was successfully installed

Btw, I had this error directly after updating Spyder to v4.0.0. The error was in a plot of a confusion matrix. This was mentioned already some months ago.… which is already linked to this seaborn question.

@ImportanceOfBeingErnest 2019-07-08 21:25:09

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(...))

@SozDaneron 2019-08-06 15:17:07

I have seen this question around but am unfamiliar how to revert back to matplotlib 3.1.0 or set the heatmap limits manually (tried this but still truncated) and can't wait for 3.1.2. How could I revert back to matplotlib 3.1.0?

@ImportanceOfBeingErnest 2019-08-06 15:20:43

It depends on how you installed matplotlib. E.g. via pip see this.

@SozDaneron 2019-08-06 15:24:12

Right, I am still new to PyCharm. Figured it out now, thanks.

@talha06 2019-08-15 02:04:56

@ImportanceOfBeingErnest I think you mean setting the plot size by the third option "set the heatmap limits manually". I have tried several sizes of figures empirically, none of them has displayed the heatmap properly.

@ImportanceOfBeingErnest 2019-08-15 02:09:21

@talha06 No I mean the plot limits. If ax = sns.heatmap(...), set ax.set_ylim(...) to whatever you need your limits to be.

@talha06 2019-08-15 02:13:26

Did not work for me, the first row is still partially (~half of it) displayed. @ImportanceOfBeingErnest

@ImportanceOfBeingErnest 2019-08-15 02:17:25

@talha06 I do not know your limits, but it should work.

@talha06 2019-08-15 02:18:37

I have 7 target classes, so I set the y limit to 7.

@talha06 2019-08-15 02:43:30

Edit: Worked when I set a colormap explicitly. Thanks for your help, @ImportanceOfBeingErnest.

@Dzamo Norton 2019-10-16 03:29:16

For 7 levels I had to use ax.set_ylim(0 ,7). Using just ax.set_ylim(7) left one row halved.

@Sidak 2019-11-04 14:34:18

For those looking to revert the version of matplotlib, use pip install 'matplotlib<=3.1.0'. (of course assuming you use pip).

@rustyDev 2019-11-18 23:06:29

matplotlib 3.1.2 is out - It is available in the Anaconda cloud via conda-forge but I was not able to install it via conda install. The manual alternative worked: Download matplotlib 3.1.2 from github and install via pip

 % curl --output matplotlib-3.1.2.tar.gz
 % pip install matplotlib-3.1.2.tar.gz

@Jade Cacho 2019-12-17 08:21:43

I am not able to update the package. I received this error: ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'c:\\users\\w-book\\anaconda3\\lib\\site-packages\\matplotli‌​b\\backends\\_backen‌​d_agg.cp37-win_amd64‌​.pyd' Consider using the --user option or check the permissions.

@rustyDev 2019-12-18 15:21:11

The above was tried in MacOS. Not familiar with Windows scenario but seems it is a local permission problem.

@Jade Cacho 2019-12-19 04:25:32

Thank you for the response. I ended up installing the older version (matplotlib-3.1.0).

@purushotam radadia 2019-11-16 07:24:10

It happens with matplotlib version 3.1.1 as suggested by importanceofbeingernest

Following solved my problem

pip install matplotlib==3.1.0

@Nikhil Pakki 2019-09-30 10:02:57

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)

@Sidak 2019-11-04 14:36:28

This, for instance, did not work me. But to be fair, my problem was different in that an entire row of the heatmap was missing. For me reverting the version, as I mentioned in a comment above, was the only way out.

@Eric Duminil 2020-01-19 14:18:18

It works, even though it seems illogical. Why should bottom be larger than top?

@lbarbus 2019-09-07 16:43:08

Fixed using the above and setting the heatmap limits manually.


ax = sns.heatmap(...

checked the current axes with

(5.5, 0.5)

Fixed with

ax.set_ylim(6.0, 0)

Related Questions

Sponsored Content

10 Answered Questions

12 Answered Questions

[SOLVED] How to change the font size on a matplotlib plot

18 Answered Questions

[SOLVED] Save plot to image file instead of displaying it using Matplotlib

  • 2012-03-08 17:38:10
  • Homunculus Reticulli
  • 1485353 View
  • 1085 Score
  • 18 Answer
  • Tags:   python matplotlib plot

7 Answered Questions

[SOLVED] Seaborn plots not showing up

8 Answered Questions

[SOLVED] Remove xticks in a matplotlib plot?

8 Answered Questions

[SOLVED] Hiding axis text in matplotlib plots

6 Answered Questions

[SOLVED] Why is my xlabel cut off in my matplotlib plot?

  • 2011-07-21 09:37:24
  • Andrew
  • 222091 View
  • 271 Score
  • 6 Answer
  • Tags:   python matplotlib

3 Answered Questions

[SOLVED] When to use cla(), clf() or close() for clearing a plot in matplotlib?

  • 2011-11-21 14:38:03
  • southoz
  • 647058 View
  • 508 Score
  • 3 Answer
  • Tags:   matplotlib plot

6 Answered Questions

[SOLVED] Plot logarithmic axes with matplotlib in python

1 Answered Questions

[SOLVED] Plotting with seaborn using the matplotlib object-oriented interface

Sponsored Content