mwaskom / seaborn

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

Heatmap twinx problem when square is True #3779

Closed the-pawel-wojcik closed 2 weeks ago

the-pawel-wojcik commented 2 weeks ago

Hello!

Thank for building seaborn. It's plots are very pretty and quick to make.

I got myself into a problem when trying to add a second y axis to the heatmap. See this

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

rng = np.random.default_rng(seed=1)
data = rng.standard_normal(size=(25, 5))
fig, ax = plt.subplots()
sbs.heatmap(data, ax=ax)  # All good
# sbs.heatmap(data, ax=ax, square=True)  # Problem
tax = ax.twinx()
plt.show()

See attached figures. When I set the square=True option. The addition of the second ax breaks the display: square_twinx square_no_twinx

What I would like to get by adding the second ax is to keep the setup as you get when you only set square=True but with the chance to modify the ticks on the right. Something of the kind that you get when you use twinx but without using the square=True option: no_square_twinx

Thanks Pawel

mwaskom commented 2 weeks ago

I'm not quite sure I understand exactly what you're trying to do, but all square=True does is call ax.set_aspect("equal") on your axes. This is probably something you can make work using the matplotlib API directly before or after plotting with seaborn.