square plots with linear equal axes -- help

Hi
This should be so simple, but I have been struggling for a long time trying
to create a square plot of exact dimensions (so a series of them can be
overlaid later), with x and y axis limits identical and set by me
(preferably without the plot limits updating automatically as I add or
delete data, but that is a separate issue), while working interactively on a
single plot using pyplot in ipython on OS X using the current enthought
distribution.

with pyplot imported as plt,
typical of what I have tried are many variations on

plt.plot(*args) with args a list of x,y
sets
fig = plt.gcf()
fig.set_size_inches(6,6,forward='True') to force a redraw
ax = plt.gca()
ax.grid(True)
plt.axis([1.0,10.0,1.0,10.0])
plt.draw()

Sometimes the x axis is set correctly, but the y axis is not, and is
typically showing more range (approximately 0.8-10.1 for example), possibly
because the plot is not square, though it is close, or because the scales
are not equal.

I have tried adding 'equal' to the plt.axis command,
and entering the values as
plt.axis(xmin=1.0,xmax=10.0,ymin=1.0,ymax=10.0)
and entering just the first three and then
plt.axis('equal')
and I have tried working with
ax.set_aspect('equal')

But, I am truly lost as I try to sort out which elements are in control.
Any help or leads would be greatly appreciated.
Scanning old archives and googling has not yet got me there.

kersey

···


View this message in context: http://www.nabble.com/square-plots-with-linear-equal-axes-----help-tp24638812p24638812.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

kbkb wrote:

Hi
This should be so simple, but I have been struggling for a long time
trying to create a square plot of exact dimensions (so a series of them
can be overlaid later), with x and y axis limits identical and set by me
(preferably without the plot limits updating automatically as I add or
delete data, but that is a separate issue), while working interactively on
a single plot using pyplot in ipython on OS X using the current enthought
distribution.

with pyplot imported as plt,
typical of what I have tried are many variations on

plt.plot(*args) with args a list of
x,y sets
fig = plt.gcf()
fig.set_size_inches(6,6,forward='True') to force a redraw
ax = plt.gca()
ax.grid(True)
plt.axis([1.0,10.0,1.0,10.0])
plt.draw()

Sometimes the x axis is set correctly, but the y axis is not, and is
typically showing more range (approximately 0.8-10.1 for example),
possibly because the plot is not square, though it is close, or because
the scales are not equal.

I have tried adding 'equal' to the plt.axis command,
and entering the values as
plt.axis(xmin=1.0,xmax=10.0,ymin=1.0,ymax=10.0)
and entering just the first three and then
plt.axis('equal')
and I have tried working with
ax.set_aspect('equal')

But, I am truly lost as I try to sort out which elements are in control.
Any help or leads would be greatly appreciated.
Scanning old archives and googling has not yet got me there.

kersey

Hi kersey,

maybe it helps if you set the figure size already while you inilialise it.
I use e.g.

--CODE--

fig = figure(1, figsize=(20,7), dpi = 80)

--CODE--

to generate non squared plots. To make a square plot figsize have to be e.g
figsize=(10,10). But I do not know if this really works, maybe only the
figure has an equal aspect ratio and the real plot not.

Otherwise you can set the plot aspect ratio = equal with
matplotlib.axes.Axes.set_aspect

Details can be found here:

http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_aspect

Andreas

···


View this message in context: http://www.nabble.com/square-plots-with-linear-equal-axes-----help-tp24638812p24644304.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Thanks for trying to help Andreas.

Those suggestions did not really solve the problem, but I did realize where
I was going astray and post that information here to help others. I did see
others new to the libraries looking for similar help.

First the conceptual misunderstanding: I did not realize the 'figure size',
as set by something like
plt.gcf().set_size_inches(6,6) includes all area: white plot area, grey
surrounding area, labels, etc. Everything inside the window frame. A bad
coincidence slowed my realization. I measured the white area inside the
axis to be 480x464 pixels, which at 80 dpi worked almost for a 6x6. So, I
thought it was close but off just a bit. But, I had set the dpi to 100, and
the total frame was exactly 600x600 as it should be. The dimensions of the
plot area inside the axes was being set automatically, and just
coincidentally worked out to be 480 in one dimension.

Solution to get truly square plots of known size with axis scale set:
    fig = plt.gcf()
    fig.set_dpi(100) # or whatever you like
    fig.set_size_inches((6.0,6.0),forward=True) #for example
    # to control the fraction of the total area set aside for axis tick mark
labels, etc.
    # this is key to keeping the interior plot area square
    plt.subplots_adjust(left=0.10,bottom=0.10,right=0.95,top=0.95)
    ## plt.axis('equal') # I had this for a while but do not think it
useful
    plt.gca().grid(True)
    # to keep the axis scale from being automatically changed as line2d
objects are added or removed
    plt.gca().set_autoscale_on(False)
    plt.axis([1.0,9.0,1.0,9.0]) # set the axis scale as appropriate
    plt.show()
    fig.canvas.draw()

Cheers
Kersey

···


View this message in context: http://www.nabble.com/square-plots-with-linear-equal-axes-----help-tp24638812p24672100.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Well, I think the meaning of the axis("equal") is a bit misleading (at
least to me), but if you look at the documentation, it says that it
changes the xlimit and ylimit (limits in data coordinate), so this is
NOT what you want.
What you need is axis("scaled") or axis("image").

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis

With this, you will have square axes regardless of the figure size or
subplot params (assuming that the x- and y- data limits are same).

Regards,

-JJ

···

On Sun, Jul 26, 2009 at 8:43 PM, kbkb<kblack@...2704...> wrote:

Thanks for trying to help Andreas.

Those suggestions did not really solve the problem, but I did realize where
I was going astray and post that information here to help others. I did see
others new to the libraries looking for similar help.

First the conceptual misunderstanding: I did not realize the 'figure size',
as set by something like
plt.gcf().set_size_inches(6,6) includes all area: white plot area, grey
surrounding area, labels, etc. Everything inside the window frame. A bad
coincidence slowed my realization. I measured the white area inside the
axis to be 480x464 pixels, which at 80 dpi worked almost for a 6x6. So, I
thought it was close but off just a bit. But, I had set the dpi to 100, and
the total frame was exactly 600x600 as it should be. The dimensions of the
plot area inside the axes was being set automatically, and just
coincidentally worked out to be 480 in one dimension.

Solution to get truly square plots of known size with axis scale set:
fig = plt.gcf()
fig.set_dpi(100) # or whatever you like
fig.set_size_inches((6.0,6.0),forward=True) #for example
# to control the fraction of the total area set aside for axis tick mark
labels, etc.
# this is key to keeping the interior plot area square
plt.subplots_adjust(left=0.10,bottom=0.10,right=0.95,top=0.95)
## plt.axis('equal') # I had this for a while but do not think it
useful
plt.gca().grid(True)
# to keep the axis scale from being automatically changed as line2d
objects are added or removed
plt.gca().set_autoscale_on(False)
plt.axis([1.0,9.0,1.0,9.0]) # set the axis scale as appropriate
plt.show()
fig.canvas.draw()

Cheers
Kersey


View this message in context: http://www.nabble.com/square-plots-with-linear-equal-axes-----help-tp24638812p24672100.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users