How to decrease height of y axis in plt.imshow

Question:

I am plotting an array in matplotlib using imshow. Here is my array:

df = pd.DataFrame({'x':[1,2,2,3,3,3],'y':[1,1,2,1,2,3],'z':[3,2,4,5,1,6]})
X = df.to_numpy()

I am plotting with this:

plt.imshow(X)

Which gives me:
enter image description here

How can I edit the plot so that the height of the rows are narrower? Basically, I want to reduce the vertical dimension of each square so that the height of a square is 10% the width of each square.

I have been trying to use:

plt.figure(figsize=(w,h)

But manipulating both w and h values just makes the plot bigger or smaller, keeping the same scale.

As an example of how to height of each square should be less than the width, see this below:
enter image description here

Asked By: connor449

||

Answers:

Instead of using imshow(), use pcolor to be able to scale the axis size.

plt.pcolor(X)

Then you can apply the fisize parameter to change the aspect or to be even more strict about your plot size apply this technique.

Answered By: Jan Willem

One way to do this is to use aspect. If it is "auto" or "1", it will be in the ratio you are seeing it. Changing it to say 0.5, will make it wider.

df = pd.DataFrame({'x':[1,2,2,3,3,3],'y':[1,1,2,1,2,3],'z':[3,2,4,5,1,6]})
plt.imshow(df, aspect = 0.5)

will give you…

enter image description here

Answered By: Redox
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.