Python 如何在使用ax.axis(';equal';)时同时强制执行xlim和ylim?
我想使用Python 如何在使用ax.axis(';equal';)时同时强制执行xlim和ylim?,python,python-2.7,matplotlib,plot,Python,Python 2.7,Matplotlib,Plot,我想使用ax.axis('equal')强制在X&Y上均匀间隔,但我还想规定X和Y轴的特定范围。如果边距也是固定的,则问题是过度约束,结果显示在图的左侧。相反,如果允许边距自动增加以填补间隙,则xlim和ylim可以保持我设置的边距,同时仍然满足axis('equal')。图的右侧显示了我所追求的一个示例如何允许打印边距“浮动”? f,ax=plt.subplots(1) #open a figure ax.axis('equal') #make the axes have equal spac
ax.axis('equal')
强制在X&Y上均匀间隔,但我还想规定X和Y轴的特定范围。如果边距也是固定的,则问题是过度约束,结果显示在图的左侧。相反,如果允许边距自动增加以填补间隙,则xlim
和ylim
可以保持我设置的边距,同时仍然满足axis('equal')
。图的右侧显示了我所追求的一个示例如何允许打印边距“浮动”?
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
图1:上述代码片段的输出
ax.set_aspect('equal',adjustable='box')
您应该将
设置ylim
,设置ylim
放在末尾,以避免自动重新缩放。@d如果我在ax.set ylim()
之后删除所有内容,它将不起作用,如果我在其他所有操作完成后将设置ylim(),设置ylim()
复制到末尾,它将不起作用。这是因为“ax.axis('equal')). 它修复了步进,因此子地块大小必须符合您的限制,这意味着,反过来,您的图形大小必须匹配,或者子地块参数除非边距自动调整大小,这就是我所要求的。有趣且相关的是:autoscale()中的tight=True
关键字
设置xlim
或ylim
以便绘图框容纳数据plt.autoscale(enable=True,axis='both',tight=True)
我知道这必须这么简单。顺便说一下,这可以取代原来问题中的ax.axis('equal')
。adjustable='box-forced'
可能更可取,尤其是在共享轴时。看见