mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.42k stars 1.91k forks source link

Suggestion: let clip_on default to False for sns.heatmap #3228

Closed jhncls closed 1 year ago

jhncls commented 1 year ago

(Tested with Seaborn 0.12.2)

When lines are drawn between cells in a heatmap (using linewidths=), the lines on the outside are shown thinner than the inner lines. They are clipped by the axes. It could be handy to automatically set clip_on=False by default.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

data = np.random.rand(2, 5)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 3))
sns.heatmap(data=data, linewidths=5, linecolor='k', cmap='autumn', cbar=False, ax=ax1)
ax1.set_title('Default: clip_on=True')
sns.heatmap(data=data, linewidths=5, clip_on=False, linecolor='k', cmap='autumn', cbar=False, ax=ax2)
ax2.set_title('Setting clip_on=False')
plt.tight_layout()
plt.show()

image

mwaskom commented 1 year ago

I don't really like how you get a gap where the lines join:

image

Normally this could be handled with the capstyle of the lines but that argument doesn't seem to be used for lines comprising the edges of a quadmesh.

Given that this is not a strictly better approach (i.e. it gets rid of one artifact but introduces a new one) and that it has the potential to surprise in other ways (e.g., your ticks almost disappear, which might not be what you're expecting), I think it's probably not a strong candidate for magical behavior.

jhncls commented 1 year ago

Indeed, as often, the gritty details are more complex than what's visible at first sight. It's better to wait until matplotlib draws this quadmesh border without the disturbing gap. My example plot used the default matplotlib settings (which has ticks, unlike many seaborn settings) and a rather thick line to emphasize the difference between outer and inner lines (which sideways also emphasizes the gap at the top left).

Seaborn's examples with white grid lines and a white background are probably the preferred aesthetics, as the attention needs to go to the cell contents, not the grid.