[Matplotlib-users] please document imshow axes order

Jody Klymak jklymak at uvic.ca
Thu Jun 6 12:35:13 EDT 2019


I think a useful way to think about it is to ask how you’d present an x-y array of numbers if you wrote them in a table.  If you saw a table:

1 3 4 7
2 7 8 1
5 9 6 5

and you said this was an array in x and y, I think most people would naturally assume x increases column-wise (i.e. the x-axis of the table) and y increases row-wise (i.e. the y-axis of the table).  Of course the opposite is another sometimes used convention, so its not like there is any fast rule, but I find that a useful justification for the convention numpy/matplotlib use.

C uses the column-major order, but FORTRAN, (and hence Matlab, Numpy, and most other scientific software packages) use row-major:

https://en.wikipedia.org/wiki/Row-_and_column-major_order

ie in numpy:

```
In [4]: a = np.array([[1, 2, 3],[4, 5, 6]])

In [6]: print(a)
[[1 2 3]
 [4 5 6]]
```

Cheers,   Jody



> On 6 Jun 2019, at 07:11, Neal Becker <ndbecker2 at gmail.com> wrote:
> 
> Actually I found this pdf
> https://www.mubdirahman.com/assets/lecture-6---advanced-plotting.pdf <https://www.mubdirahman.com/assets/lecture-6---advanced-plotting.pdf>
> starting at page 6 explained things quite well
> 
> On Thu, Jun 6, 2019 at 10:02 AM Saito Kotaro (PSI) <kotaro.saito at psi.ch <mailto:kotaro.saito at psi.ch>> wrote:
>> 
>> I went through the exact same issue some years ago and wrote a note about this confusing definition of the directions in 2D arrays.
>> It’s written in Japanese but I think it's still understandable for most of you who can read matplotlib and numpy codes.
>> https://qiita.com/skotaro/items/1bc4db69b53210d839ec
>> 
>> I hope this would help for improving a tutorial or the documentation.
>> 
>> Best regards,
>> 
>> Kotaro
>> 
>> //================//================//
>> Paul Scherrer Institut
>> Kotaro SAITO 斉藤耕太郎
>> Laboratory for Neutron Scattering and Imaging
>> WHGA/348
>> 5232 Villigen PSI, Schweiz
>> +41 56 310 3179
>> kotaro.saito at psi.ch
>> https://sites.google.com/view/kotarosaito/
>> //================//================//
>> 
>>> On 2019/6/ 6, at 15:42, Neal Becker <ndbecker2 at gmail.com> wrote:
>>> 
>>> My suggestion is to enhance the documentation.  If there is a nice
>>> tutorial, adding a link to it would be great.
>>> Thanks,
>>> Neal
>>> 
>>> On Thu, Jun 6, 2019 at 9:39 AM Thomas Caswell <tcaswell at gmail.com> wrote:
>>>> 
>>>> There is also a tutorial addressing the ways that `origin` and `extent` interact with each other: https://matplotlib.org/tutorials/intermediate/imshow_extent.html
>>>> 
>>>> As for the transpose, that is due to one branch of math teaching use to think (x, y) and different branch of math teaching us to think (row, column).  For `imshow` we use the second one which is consistent with how numpy's repr of on array.  It would be confusing if the data for `my_array[0, :]` was displayed as the first _column_ of the image.
>>>> 
>>>> I do not disagree that this is frustrating/confusing, there is a comment in some of my grad school code which is basically "getting the (x,y) vs (r,c) conversions right here took you an afternoon, don't touch this again!" ;)
>>>> 
>>>> Tom
>>>> 
>>>> On Thu, Jun 6, 2019 at 9:31 AM Nathan Goldbaum <nathan12343 at gmail.com> wrote:
>>>>> 
>>>>> This is discussed in the description of the "origin" keyword argument:
>>>>> 
>>>>> origin : {'upper', 'lower'}, optional
>>>>> 
>>>>> Place the [0,0] index of the array in the upper left or lower left corner of the axes. The convention 'upper' is typically used for matrices and images. If not given, rcParams["image.origin"] is used, defaulting to 'upper'.
>>>>> 
>>>>> Note that the vertical axes points upward for 'lower' but downward for 'upper'.
>>>>> 
>>>>> 
>>>>> On Thu, Jun 6, 2019 at 9:25 AM Neal Becker <ndbecker2 at gmail.com> wrote:
>>>>>> 
>>>>>> I just wasted quite a bit of time trying to debug my code.  It took a long
>>>>>> time because I was using imshow to debug, and didn't know that imshow
>>>>>> displays a matrix not using cartesian coordinates.  The axes are transposed
>>>>>> and one axis is reversed.  The documentation doesn't mention this.
>>>>>> 
>>>>>> Thanks,
>>>>>> Neal
>>>>>> 
>>>>>> _______________________________________________
>>>>>> Matplotlib-users mailing list
>>>>>> Matplotlib-users at python.org
>>>>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>>>> 
>>>>> _______________________________________________
>>>>> Matplotlib-users mailing list
>>>>> Matplotlib-users at python.org
>>>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>>> 
>>>> 
>>>> 
>>>> --
>>>> Thomas Caswell
>>>> tcaswell at gmail.com
>>> 
>>> 
>>> 
>>> --
>>> Those who don't understand recursion are doomed to repeat it
>>> _______________________________________________
>>> Matplotlib-users mailing list
>>> Matplotlib-users at python.org
>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>> 
> 
> 
> -- 
> Those who don't understand recursion are doomed to repeat it
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org <mailto:Matplotlib-users at python.org>
> https://mail.python.org/mailman/listinfo/matplotlib-users <https://mail.python.org/mailman/listinfo/matplotlib-users>
--
Jody Klymak    
http://web.uvic.ca/~jklymak/





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190606/2f252a8e/attachment.html>


More information about the Matplotlib-users mailing list