如何在使用 ax.axis('equal') 时同时执行 xlim 和 ylim? [英] How to enforce both xlim and ylim while using ax.axis('equal')?

查看:37
本文介绍了如何在使用 ax.axis('equal') 时同时执行 xlim 和 ylim?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ax.axis('equal')强制在X&Y,但我也想为 X 和 Y 轴指定特定范围.如果边距也固定了,则问题被过度约束,结果如图左侧所示 图 1:上述代码片段的输出.

解决方案

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

I want to use ax.axis('equal') to force even spacing on X & Y, but I also want to prescribe specific ranges for the X and Y axes. If the margins are also fixed, the problem is over constrained and the result is shown on the left side of the Figure 1. If instead, the margins were allowed to automatically increase themselves to take up the slack, then xlim and ylim could stay as I set them while still satisfying axis('equal'). An example of what I'm after is shown on the right side of Figure 1. How can I allow the plot margins to "float"?

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

Figure 1: output from the above code snippet.

解决方案

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

这篇关于如何在使用 ax.axis('equal') 时同时执行 xlim 和 ylim?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆