1

I am using pcolormesh to create a heatmap.

heatmap = plt.pcolormesh(
    grid,
    edgecolors="k",
    cmap=colors.ListedColormap(
        [
            "white",
            "red",
            "blue",
            "green",
            "orange",
            "black",
            "purple",
            "yellow",
            "brown",
            "violet",
            "gray",
        ]
    ),
    linewidth=2,
)
ax = plt.gca()
ax.set_aspect("auto")

I want to set the cells shape to be rectangular. How can I do that?

YNWA
  • 45
  • 7

1 Answers1

2

I imagine you meant "square" cells, not "rectangular":

Set aspect to 1: ax.set_aspect(1)

square cells

aspect defines the Y/X ratio, so to have twice as high cells: ax.set_aspect(2)

twice as high

And twice as wide: ax.set_aspect(1/2)

twice as wide

mozway
  • 194,879
  • 13
  • 39
  • 75
  • Now that I have updated my post with the image of the heatmap in the default view I have, I want to force each cells to be rectangular. The cells change their shape based on aspect ration i.e. when I enlarge the window size then they can be rectangular but I want them to be rectangular in any window shape. – YNWA Aug 25 '21 at 09:28
  • yes, but rectangular **how**? cells are always rectangular (not circle, triangle…) – mozway Aug 25 '21 at 09:29
  • Thank you so much, ax.set_aspect(1/2) did the trick for me. – YNWA Aug 25 '21 at 09:30