Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip x and y axes for Matplotlib imshow()

I'm using pyplot with matplotlib, and I would like to display some data as an image. When I use imshow() the data is flipped from the way I want to view it. How would I switch the x and y axes, either with imshow() or to the numpy array before I send it to imshow()?

(i.e. I want the horizontal axis to be vertical)

I've tried using origin='upper' and origin='lower' in the imshow() command, but that just reverses one axis instead of switching them around

I've also tried using reshape on the data, but the order gets all messed up

like image 206
Brent Avatar asked Aug 14 '13 16:08

Brent


People also ask

How do you flip axes of Imshow?

Use ax. invert_yaxis() to invert the y-axis, or ax. invert_xaxis() to invert the x-axis. Save this answer.

How do I flip the X-axis in MatPlotLib?

Most common method is by using invert_xaxis() and invert_yaxis() for the axes objects. Other than that we can also use xlim() and ylim(), and axis() methods for the pyplot object. To invert X-axis and Y-axis, we can use invert_xaxis() and invert_yaxis() function.

How do you normalize Imshow?

Just specify vmin=0, vmax=1 . By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

How do you change the x and y-axis in Python?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.


1 Answers

To close out the question-

You need to transpose the numpy array before passing it to matplotlib:

>>> a
array([[0, 1],
       [2, 3]])
>>> a=a.T
>>> a
array([[0, 2],
       [1, 3]])

So using plt it should simply be:

plt.imshow(a.T)
like image 178
Daniel Avatar answered Sep 19 '22 20:09

Daniel