When the cbar is on, the cells in seaborn's heatmap are rectangular. I could change the figsize of my axis, but I am wondering if there is an easier way to keep the cells square.
Asked
Active
Viewed 7,219 times
12
-
Have a look at `plt.axis('equal')` – Martin Evans Mar 21 '17 at 07:35
-
2Have you tried the `square` argument? http://seaborn.pydata.org/generated/seaborn.heatmap.html – mwaskom Mar 21 '17 at 13:54
-
@mwaskom oh that is embarrassing – Demetri Pananos Mar 21 '17 at 15:45
-
Don't fret — there are very many! – mwaskom Mar 21 '17 at 18:09
-
I tried `square=True` and `ax.set_aspect("equal")` but neither doesn't fix bottom and top rows' units. Any idea how to fix this? – Rylan Schaeffer Jan 23 '20 at 00:43
-
@RylanSchaeffer Are they cut off? – Demetri Pananos Jan 23 '20 at 02:53
-
My top and bottom row's cells are rectangular, with their widths matching the the other columns, but their heights are cut in half. – Rylan Schaeffer Jan 23 '20 at 17:40
-
1@RylanSchaeffer This is a bug in seaborn https://github.com/mwaskom/seaborn/issues/1773. I think this can be rectified by changing the `ylim` of the axes. The solution should be found in this github issue. – Demetri Pananos Jan 23 '20 at 17:44
2 Answers
11
seaborn heatmap has the option square:
heatmap(data, square=True)

Olivetree
- 415
- 1
- 4
- 8
-
1I tried `square=True` and `ax.set_aspect("equal")` but neither doesn't fix bottom and top rows' units. Any idea how to fix this? – Rylan Schaeffer Jan 23 '20 at 00:43
7
You can use ax.set_aspect("equal")
to set an equal aspect ratio for the axes ax
.
Example:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data = np.random.rand(4,16)
ax = sns.heatmap(data)
ax.set_aspect("equal")
plt.show()

ImportanceOfBeingErnest
- 321,279
- 53
- 665
- 712
-
1I tried `square=True` and `ax.set_aspect("equal")` but neither doesn't fix bottom and top rows' units. Any idea how to fix this? – Rylan Schaeffer Jan 23 '20 at 00:43
-
-
My top and bottom row's cells are rectangular, with their widths matching the the other columns, but their heights are cut in half. – Rylan Schaeffer Jan 23 '20 at 17:40
-
1Right. That is a [totally different problem](https://stackoverflow.com/questions/56942670/matplotlib-seaborn-first-and-last-row-cut-in-half-of-heatmap-plot/56942725#56942725). – ImportanceOfBeingErnest Jan 23 '20 at 17:41
-
Thanks for the link! I wouldn't say it's a "totally" different problem - like OP, I'm also trying to ensure the cells remain square. – Rylan Schaeffer Jan 23 '20 at 17:44
-
It is, because your problem is independent of the colorbar. – ImportanceOfBeingErnest Jan 23 '20 at 21:46