Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python. Matplotlib inverted image

I haven't an idea what is wrong here.

import matplotlib.pyplot as plt

im = plt.imshow(plt.imread('tas.png'))
plt.show()

And the Y axis has inverted.
So I wrote an argument origin='lower'.

im = plt.imshow(plt.imread('tas.png'), origin='lower')
plt.show()

And what I have got.
The Y axis came normally, but image now has been inverted.

Also when i try to re-scale axis X and Y, the image hasn't got smaller, only cut out piece.

Thank you in advance. I would be so grateful for some help.

like image 320
Protoss Reed Avatar asked Jan 29 '13 18:01

Protoss Reed


People also ask

How do I invert an image in Matplotlib?

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

How do I flip a plot in Matplotlib?

Matplotlib invert x and y axisBy using invert_xaxis() and invert_yaxis() method we can flip the x-axis and y-axis respectively. In the above example, we import numpy and matplotlib libraries. After this, we define data on x and y coordinates. By using the plt.

How do you plot a dynamic graph in Python?

Practical Data Science using Python Make a list of data points and colors. Plot the bars with data and colors, using bar() method. Using FuncAnimation() class, make an animation by repeatedly calling a function, animation, that sets the height of the bar and facecolor of the bars.

Which function is used to specify the starting and ending points for Y axis in Python?

Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively.


1 Answers

You are running into an artifact of how images are encoded. For historical reasons, the origin of an image is the top left (just like the indexing on 2D array ... imagine just printing out an array, the first row of your array is the first row of your image, and so on.)

Using origin=lower effectively flips your image (which is useful if you are going to be plotting stuff on top of the image). If you want to flip the image to be 'right-side-up' and have the origin of the bottom, you need to flip your image before calling imshow

 import numpy as np
 im = plt.imshow(np.flipud(plt.imread('tas.png')), origin='lower')
 plt.show()
like image 105
tacaswell Avatar answered Sep 21 '22 12:09

tacaswell