[英]How can I get the actual axis limits when using ax.axis('equal')?
I am using ax.axes('equal')
to make the axis spacing equal on X and Y, and also setting xlim
and ylim
. 我正在使用ax.axes('equal')
使X和Y上的轴间距相等,并且还设置了xlim
和ylim
。 This over-constrains the problem and the actual limits are not what I set in ax.set_xlim()
or ax.set_ylim()
. 这过度约束了问题,实际限制不是我在ax.set_xlim()
或ax.set_ylim()
。 Using ax.get_xlim()
just returns what I provided. 使用ax.get_xlim()
只会返回我提供的内容。 How can I get the actual visible limits of the plot? 如何获得该图的实际可见极限?
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
What commands will let me draw the green box around the actual edges of the figure? 哪些命令可以让我在图的实际边缘周围绘制绿色框?
Related: Force xlim
, ylim
, and axes('equal')
at the same time by letting margins auto-adjust 相关: 通过自动调整边距来同时强制xlim
, ylim
和ylim
axes('equal')
The actual limits are not known until the figure is drawn. 绘制该图之前,未知实际极限。 By adding a canvas draw after setting the xlim
and ylim
, but before obtaining the xlim
and ylim
, then one can get the desired limits. 通过在设置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)
Not to detract from the accepted answer, which does solve the problem of getting updated axis limits, but is this perhaps an example of the XY problem? 不减损接受的答案,这确实解决了获取更新的轴限制的问题,但这也许是XY问题的一个例子吗? If what you want to do is draw a box around the axes, then you don't actually need the xlim
and ylim
in data coordinates. 如果要在轴周围绘制一个框,则实际上不需要数据坐标中的xlim
和ylim
。 Instead, you just need to use the ax.transAxes
transform which causes both x
and y
data to be interpreted in normalized coordinates instead of data-centered coordinates: 相反,您只需要使用ax.transAxes
转换即可使x
和y
数据都以规范化坐标而不是以数据为中心的坐标进行解释:
ax.plot([0,0,1,1,0],[0,1,1,0,0], lw=3, transform=ax.transAxes)
The great thing about this is that your line will stay around the edges of the axes even if the xlim
and ylim
subsequently change . 这样做的xlim
,即使xlim
和ylim
随后发生变化 ,您的直线也将停留在轴的边缘。
You can also use transform=ax.xaxis.get_transform()
or transform=ax.yaxis.get_transform()
if you want only x
or only y
to be defined in normalized coordinates, with the other one in data coordinates. 如果只想在标准化坐标中定义x
或y
,而在数据坐标中定义另一个y
,则也可以使用transform=ax.xaxis.get_transform()
或transform=ax.yaxis.get_transform()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.