Reputation: 6938
Let's say that I want to plot an ellipse in matplotlib:
import numpy as np
import matplotlib.pyplot as plt
x = np.sin(np.linspace(0,2*np.pi,100))
y = 2*np.cos(np.linspace(0,2*np.pi,100))
ax = plt.subplot(111)
ax.plot(x,y)
plt.show()
The result looks like a circle:
I can get the result that I want by adding five lines of code:
import numpy as np
import matplotlib.pyplot as plt
x = np.sin(np.linspace(0,2*np.pi,100))
y = 2*np.cos(np.linspace(0,2*np.pi,100))
ax = plt.subplot(111)
ax.plot(x,y)
#make the axes square and equal
ax.set_aspect('equal')
minimum = np.min((ax.get_xlim(),ax.get_ylim()))
maximum = np.max((ax.get_xlim(),ax.get_ylim()))
ax.set_xlim(minimum*1.2,maximum*1.2)
ax.set_ylim(minimum*1.2,maximum*1.2)
plt.show()
But this seems very clunky. Is there a function built into matplotlib to give the expected behavior automagically?
Upvotes: 3
Views: 5210
Reputation: 63
If it is of any use, here is the similar code for MATLAB:
% Make axes equal in size and lenght
% ax3 is an axe() object.
axis (ax3,'auto')
axis (ax3,'square')
minimum = min(abs([ax3.XLim,ax3.YLim]));
maximum = max(abs([ax3.XLim,ax3.YLim]));
limit = max(minimum,maximum);
limit = round(limit,0);
axis(ax3,[-limit limit -limit limit]);
ticks = [-limit:limit/5:limit];
xticks(ax3, ticks);
yticks(ax3,ticks);
grid(ax3,'on')
Upvotes: 0
Reputation: 118
A quick google threw this up: http://matplotlib.org/examples/pylab_examples/equal_aspect_ratio.html. I haven't tested it myself, though.
ax.set_aspect('equal', 'datalim')
ax.margins(0.1)
Upvotes: 5