让我们说我想在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()
我可以通过添加五行代码来获得我想要的结果:
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()
但这看起来非常笨重。是否在matplotlib中内置了一个自动提供预期行为的函数?
答案 0 :(得分:1)
快速谷歌把它扔了:http://matplotlib.org/examples/pylab_examples/equal_aspect_ratio.html。 不过,我自己没有测试过。
ax.set_aspect('equal', 'datalim')
ax.margins(0.1)
答案 1 :(得分:0)
如果有用,这是 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')