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
Use ax. invert_yaxis() to invert the y-axis, or ax. invert_xaxis() to invert the x-axis. Save this answer.
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.
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).
MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With