使用 ax.axis('equal') 时如何获得实际的轴限制?

EL_*_*DON 6 python plot matplotlib python-2.7

我正在使用ax.axes('equal')使 X 和 Y 上的轴间距相等,并设置xlimylim。这过度限制了问题,实际限制不是我在ax.set_xlim()或 中设置的ax.set_ylim()。使用ax.get_xlim()只返回我提供的内容。如何获得绘图的实际可见限制?

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
Run Code Online (Sandbox Code Playgroud)

什么命令可以让我在图形的实际边缘周围绘制绿色框?

绘图显示 get_xlim() 和 get_ylim() 的结果与图的可见边界不匹配

相关阅读:xlimylim以及axes('equal')在通过让同时利润率自动调整

esm*_*mit 5

在绘制图形之前,我们无法得知实际的限制。xlim通过在设置和后ylim但在获取xlim和之前添加画布绘制ylim,可以获得所需的限制。

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])

#Drawing is crucial 
f.canvas.draw() #<---------- I added this line

#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) 
Run Code Online (Sandbox Code Playgroud)

脚本生成的图