10

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.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Protoss Reed
  • 265
  • 2
  • 6
  • 16
  • When I use your code everything works fine. What version of python/matplotlib you're using. Also do the second 'inverted' has the same meaning as the first in your question? – Bula Jan 29 '13 at 18:32
  • I know code works great but not right. In the first part of code image look right, But Y axix begin from 100 to 0. When i wrote an argument (origin = 'lower' or ax = plt.gca() ax.invert_yaxis() ) Y axis became from 0 to 100, but image going to be inveted. – Protoss Reed Jan 29 '13 at 18:45

1 Answers1

21

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()
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • +1 for this. Alternatively the OP could reverse the tick labels, but that's far from an ideal solution. – Paul H Jan 29 '13 at 19:56
  • @PaulH Manually reversing the ticks can only cause problems later if you plot stuff on top of the image or you get coordinates out of any sort of image processing. – tacaswell Jan 29 '13 at 20:09