imshow, matshow coordinate system

Hi there,

I'm pretty new to python; I'm in the process of switching from Matlab.
I do quite a bit of image processing in my research, and while
pylab/matplotlib seems to be a great plotting library, some of the
quirks seem just a little bit frustrating - I'm hoping for some
enlightenment :slight_smile:

When I use imshow or matshow to display an array, the points with
index (m, n) are displayed with the pixels centred at (0.5 + m, 0.5 +
n). Is there a setting somewhere to make it so that the centres of the
pixels are at the index values rather than their bottom left corners?
Or must I be always adding 0.5 to things to make them appear in the
right places?

Thanks, I appreciate any help

Richard

路路路

--
Richard Brown
Ph.D. Candidate
Dept. of Mechanical Engineering
University of Canterbury, NZ

Richard Brown wrote:

Hi there,

I'm pretty new to python; I'm in the process of switching from Matlab.
I do quite a bit of image processing in my research, and while
pylab/matplotlib seems to be a great plotting library, some of the
quirks seem just a little bit frustrating - I'm hoping for some
enlightenment :slight_smile:

When I use imshow or matshow to display an array, the points with
index (m, n) are displayed with the pixels centred at (0.5 + m, 0.5 +
n). Is there a setting somewhere to make it so that the centres of the
pixels are at the index values rather than their bottom left corners?
Or must I be always adding 0.5 to things to make them appear in the
right places?

Coincidentally, a few days ago I made this change for matshow; I had made it in spy some time ago. I have so far left imshow alone; isn't its present behavior consistent with Matlab? That is not necessarily a good reason for leaving it the way it is, but it is reason for some caution. I suspect quite a few people may prefer it the way it is; let's see who responds, and what opinions are voiced.

One way of maintaining compatibility (if it is indeed compatible now, and if this is judged desirable) would be to add a kwarg and/or rc option. But if there is a consensus that imshow should by default put index ticks at cell centers, then I will be happy to make that change.

Eric

路路路

Thanks, I appreciate any help

Richard

Richard Brown wrote:
> Hi there,
>
> I'm pretty new to python; I'm in the process of switching from Matlab.
> I do quite a bit of image processing in my research, and while
> pylab/matplotlib seems to be a great plotting library, some of the
> quirks seem just a little bit frustrating - I'm hoping for some
> enlightenment :slight_smile:
>
> When I use imshow or matshow to display an array, the points with
> index (m, n) are displayed with the pixels centred at (0.5 + m, 0.5 +
> n). Is there a setting somewhere to make it so that the centres of the
> pixels are at the index values rather than their bottom left corners?
> Or must I be always adding 0.5 to things to make them appear in the
> right places?

Coincidentally, a few days ago I made this change for matshow; I had
made it in spy some time ago. I have so far left imshow alone; isn't
its present behavior consistent with Matlab? That is not necessarily a
good reason for leaving it the way it is, but it is reason for some
caution. I suspect quite a few people may prefer it the way it is;
let's see who responds, and what opinions are voiced.

Thanks for your timely response. Let me give you a few examples to
clarify the things which I think might be relevant issues to address.
(numpy and pylab imported)

PRELIMINARIES
# Create a 6x6 logical array with a 2x2 square near the to left
xx = zeros((6, 6), dtype='Bool')
xx[1:3, 1:3] = True

EXAMPLE 1 - imshow
Trying to plot a point which should appear on the square:

imshow(xx, interpolation='nearest')
plot([2],[2], 'y.', markersize=20)

The image looks correct, with the square in the top left, but the y
axis is labelled backwards. Therefore when I try to plot a point in
the middle of it, it misses altogether

EXAMPLE 2 - matshow (not your new version)

matshow(xx)

So far so good - the y axis is the right way around

plot([2],[2], 'y.', markersize=20)

Oops - the y axis flipped, there is a block of white at the top, and
the image is now upside-down. The point has showed up in the right
place w.r.t the image though.

EXAMPLE 3 - off by 0.5 problem - relevant to imshow too

matshow(xx)

Let's say I want to compute the centroid of the square blob. IMO a
natural way to do this is:

cen = mean(where(xx), 1)
plot([cen[0]], [cen[1]], 'y.')

This is off by 0.5 in both directions. This kind of thing is my
argument for why the coordinate system should be aligned with the
array indices.

Matlab behaviour:
In Matlab, the pixels are centred on integer coordinates corresponding
to their array index. Matlab indexing is ones based, so a 2x2 image
will have axes limits of 0.5-2.5 in each direction, with the pixel
centres at (1,1), (1,2) etc.
imshow in Matlab plots the array with the (1,1) coordinate in the top
left, and the y axis increasing from the top down (like what matshow
does here)

cheers,

Richard

路路路

On 22/03/07, Eric Firing <efiring@...202...> wrote:

Richard,

I have made the requested change to imshow, so it is consistent with the new matshow, and I think it makes much more sense this way--but note that it depends on your rc value for image.origin. So, whenever you get mpl from svn, or the next release (whenever that occurs--not very soon, I suspect), I think you will find both matshow and imshow more to your liking.

I also changed the docstring explanation for the extent kwarg, noting that it gives (left, right, bottom, top) data limits of the axes.

More doc changes and other tweaks for this class of functions will be in order, when I get back to it.

Eric

Richard Brown wrote:

路路路

On 22/03/07, Eric Firing <efiring@...202...> wrote:

Richard Brown wrote:
> Hi there,
>
> I'm pretty new to python; I'm in the process of switching from Matlab.
> I do quite a bit of image processing in my research, and while
> pylab/matplotlib seems to be a great plotting library, some of the
> quirks seem just a little bit frustrating - I'm hoping for some
> enlightenment :slight_smile:
>
> When I use imshow or matshow to display an array, the points with
> index (m, n) are displayed with the pixels centred at (0.5 + m, 0.5 +
> n). Is there a setting somewhere to make it so that the centres of the
> pixels are at the index values rather than their bottom left corners?
> Or must I be always adding 0.5 to things to make them appear in the
> right places?

Coincidentally, a few days ago I made this change for matshow; I had
made it in spy some time ago. I have so far left imshow alone; isn't
its present behavior consistent with Matlab? That is not necessarily a
good reason for leaving it the way it is, but it is reason for some
caution. I suspect quite a few people may prefer it the way it is;
let's see who responds, and what opinions are voiced.

Thanks for your timely response. Let me give you a few examples to
clarify the things which I think might be relevant issues to address.
(numpy and pylab imported)

PRELIMINARIES
# Create a 6x6 logical array with a 2x2 square near the to left
xx = zeros((6, 6), dtype='Bool')
xx[1:3, 1:3] = True

EXAMPLE 1 - imshow
Trying to plot a point which should appear on the square:

imshow(xx, interpolation='nearest')
plot([2],[2], 'y.', markersize=20)

The image looks correct, with the square in the top left, but the y
axis is labelled backwards. Therefore when I try to plot a point in
the middle of it, it misses altogether

EXAMPLE 2 - matshow (not your new version)

matshow(xx)

So far so good - the y axis is the right way around

plot([2],[2], 'y.', markersize=20)

Oops - the y axis flipped, there is a block of white at the top, and
the image is now upside-down. The point has showed up in the right
place w.r.t the image though.

EXAMPLE 3 - off by 0.5 problem - relevant to imshow too

matshow(xx)

Let's say I want to compute the centroid of the square blob. IMO a
natural way to do this is:

cen = mean(where(xx), 1)
plot([cen[0]], [cen[1]], 'y.')

This is off by 0.5 in both directions. This kind of thing is my
argument for why the coordinate system should be aligned with the
array indices.

Matlab behaviour:
In Matlab, the pixels are centred on integer coordinates corresponding
to their array index. Matlab indexing is ones based, so a 2x2 image
will have axes limits of 0.5-2.5 in each direction, with the pixel
centres at (1,1), (1,2) etc.
imshow in Matlab plots the array with the (1,1) coordinate in the top
left, and the y axis increasing from the top down (like what matshow
does here)

cheers,

Richard

Hi Eric

Thanks heaps - that was exactly what I was hoping for.

A small thing - I think the change might have induced a slight problem
in matshow - e.g. for a 3x3 image, the pixels are in the right place,
but the axis limits need to change from 0...3 to -0.5...2.5.

I'm impressed with the speed of the change - seems like a very helpful
community here.

cheers,

Richard

路路路

On 23/03/07, Eric Firing <efiring@...202...> wrote:

Richard,

I have made the requested change to imshow, so it is consistent with the
new matshow, and I think it makes much more sense this way--but note
that it depends on your rc value for image.origin. So, whenever you get
mpl from svn, or the next release (whenever that occurs--not very soon,
I suspect), I think you will find both matshow and imshow more to your
liking.