Seaborn: sns.heatmap top and bottom boxes are cut off

Created on 8 Jul 2019  路  6Comments  路  Source: mwaskom/seaborn

With seaborn 0.9.0 and matplotlib 3.1.1, the topmost and bottommost row of boxes in a seaborn plot are partially cut off:

import seaborn as sns
import numpy as np

np.random.seed(42)
sns.heatmap(np.random.random((10, 10)))

heatmap

As another example, consider this demo from the gallery:

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# Load the example flights dataset and conver to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")

# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax)

heatmap

This is a pretty major visual issue. Apologies if this has been reported before, but I couldn't find a record of such in the GH issues.

Most helpful comment

Here's what I figured out as a work-around:

# fix for mpl bug that cuts off top/bottom of seaborn viz
b, t = plt.ylim() # discover the values for bottom and top
b += 0.5 # Add 0.5 to the bottom
t -= 0.5 # Subtract 0.5 from the top
plt.ylim(b, t) # update the ylim(bottom, top) values
plt.show() # ta-da!

All 6 comments

This was a matplotlib regression introduced in 3.1.1 which has been fixed in 3.1.2 (still forthcoming). For now the fix is to downgrade matplotlib to a prior version.


Edit (by @mwaskom): The advice to downgrade is now several versions out of date and it is better to upgrade matplotlib to solve this issue.

conda install matplotlib=3.0.3

solution also at #14751

Here's what I figured out as a work-around:

# fix for mpl bug that cuts off top/bottom of seaborn viz
b, t = plt.ylim() # discover the values for bottom and top
b += 0.5 # Add 0.5 to the bottom
t -= 0.5 # Subtract 0.5 from the top
plt.ylim(b, t) # update the ylim(bottom, top) values
plt.show() # ta-da!

This was a matplotlib regression introduced in 3.1.1 which has been fixed in 3.1.2 (still forthcoming). For now the fix is to downgrade matplotlib to a prior version.

Matplotlib 3.1.2 has been released (also available for conda users through conda-forge using conda install -c conda-forge matplotlib=3.1.2). This fixes the issue.

This was a matplotlib regression introduced in 3.1.1 which has been fixed in 3.1.2 (still forthcoming). For now the fix is to downgrade matplotlib to a prior version.

Upgrade to Matplotlib 3.1.2 sorts the issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vinay-jayaram picture vinay-jayaram  路  3Comments

stonebig picture stonebig  路  4Comments

bondarevts picture bondarevts  路  3Comments

songololo picture songololo  路  4Comments

chanshing picture chanshing  路  3Comments