imshow defaults to inverted y axis, can't be flipped using invert_yaxis

56 views Asked by At

Whenever I generate a graph in matplotlib, the axis seems to be inverted. As an example, I created a figure from an array of 0s:

dims = [100,100] 

square = np.zeros(dims)
plt.imshow(square)

This produces the following figure:

enter image description here

Whilst doing:

dims = [100,100] 
square = np.zeros(dims)
plt.gca().invert_yaxis()
plt.imshow(square)

produces the exact same figure.

How can I make it so my y-axis starts at 0 at the origin?

1

There are 1 answers

0
Maria K On

I think invert_yaxis doesn't work because in your case Axes are empty:

plt.gca()
# > <Axes: >

You can set axis manually instead:

dims = [100,100] 
square = np.zeros(dims)
plt.gca().set_xlim([0, dims[0]])
plt.gca().set_ylim([0, dims[1]])
plt.imshow(square)

enter image description here