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
>
> 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: