如何在使用ax.axis时强制执行xlim和ylim('等于')?

时间:2016-09-29 16:38:08

标签: python python-2.7 matplotlib plot

我想使用ax.axis('equal')强制X&上的均匀间距是的,但我也想规定X轴和Y轴的具体范围。如果边距也是固定的,则问题过度约束,结果显示在图1的左侧。如果相反,允许边距自动增加以消除松弛,那么xlimylim可以在我设置它们的同时保持满足axis('equal')。我所追求的一个例子显示在图1的右侧。 如何允许情节边距为"浮动"?

f,ax=plt.subplots(1) #open a figure
ax.axis('equal') #make the axes have equal spacing
ax.plot([0,20],[0,20]) #test data set

#change the plot axis limits
ax.set_xlim([2,18]) 
ax.set_ylim([5,15])

#read the plot axis limits
xlim2=array(ax.get_xlim())
ylim2=array(ax.get_ylim())

#define indices for drawing a rectangle with xlim2, ylim2
sqx=array([0,1,1,0,0])
sqy=array([0,0,1,1,0])

#plot a thick rectangle marking the xlim2, ylim2
ax.plot(xlim2[sqx],ylim2[sqy],lw=3) #this does not go all the way around the edge

actual and desired output 图1:上面代码片段的输出。

1 个答案:

答案 0 :(得分:2)

ax.set_aspect('equal',adjustable='box')

enter image description here