OK, this does not work for me. Test case:
> d=arange(201)*pi/100 plot(cos(d),sin(d)) square() show()
> This should look like a circle. It looks like an oval.
> Problems: 1. fig.set_figsize_inches is ignored. It has no
Right, I noted this in my original post - if you continue that
post where I posted this code:
It's the right idea, but it doesn't work currently if you are
using a GUI backend. The reason it fails is has to do with the
pipeline order in which figure managers, figures etc are created
by default, and the fact that configure events and resize events
change the figure width and height (so you can for example, resize
your figure window). It *does* work if you specify the figure
size
figure(figsize=(8,8))
This is a bug, as I mentioned before, but will require some thought
about how to get this right across backends.
> 2. axis('equal') should not be desirable, if it is matlab
> compatible
> Set axis limits and aspect ratios - MATLAB axis
> That is, square() should set an axis aspect ratio of unity,
> but axis('equal') should equate the *unit length* along each
> axis. (For my example circle this is not a problem, but in
> generaly this is completely wrong.)
With the amended axes code I'm attaching below, does this example work
as you hope
from matplotlib.matlab import *
figure(figsize=(8,8))
ax = axes([0.1, 0.1, 0.8, 0.8])
d=arange(201)*pi/100
plot(10+cos(d),sin(d))
axis('equal')
show()
Perhaps more typing than you want for an interactive demo, but does
make everything clear and should work, at least up to the dpix/dpiy
error we discussed earlier.
If it works fine, I'll make it a FAQ. I still have to handle the case
where the axis lim are decreasing...
JDH
def axis(*v):
"""
axis() returns the current axis as a length a length 4 vector
axis(v) where v= [xmin xmax ymin ymax] sets the min and max of the
x and y axis limits
axis('off') turns off the axis lines and labels
axis('equal') sets the xlim width and ylim height to be to be
identical. The longer of the two intervals is chosen
"""
if len(v)==1 and is_string_like(v[0]):
s = v[0]
if s.lower()=='on': gca().set_axis_on()
elif s.lower()=='off': gca().set_axis_off()
elif s.lower()=='equal':
ax = gca()
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
width = xmax-xmin
height = ymax-ymin
# TODO: handle decreasing lim
interval = max([width, height])
ax.set_xlim((xmin, xmin+interval))
ax.set_ylim((ymin, ymin+interval))
draw_if_interactive()
else:
error_msg('Unrecognized string %s to axis; try on or off' % s)
return
try: v[0]
except IndexError:
xlim = gca().get_xlim()
ylim = gca().get_ylim()
return [xlim[0], xlim[1], ylim[0], ylim[1]]
v = v[0]
if len(v) != 4:
error_msg('v must contain [xmin xmax ymin ymax]')
return
gca().set_xlim([v[0], v[1]])
gca().set_ylim([v[2], v[3]])
draw_if_interactive()