Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. This over-constrains the problem and the actual limits are not what I set in ax.set_xlim() or ax.set_ylim(). Using ax.get_xlim() just returns what I provided. 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?

Plot showing that the results of get_xlim() and get_ylim() do not match the visible bounds of the figure

Related: Force xlim, ylim, and axes('equal') at the same time by letting margins auto-adjust

like image 584
EL_DON Avatar asked Sep 27 '16 17:09

EL_DON


People also ask

How do you limit the axis in Python?

To set axes limits use set_xlim() and set_ylim() for x-axis and y-axis respectively. To plot a line chart, use the plot() function of pyplot module. To add a title to the plot, use the title() function. To add a label at the x-axis, use the xlabel() function.

How are axis limits set?

Change Axis Limits Create a line plot. Specify the axis limits using the xlim and ylim functions. For 3-D plots, use the zlim function. Pass the functions a two-element vector of the form [min max] .

What does axis equal do in Matlab?

axis equal sets the aspect ratio so that the data units are the same in every direction. The aspect ratio of the x-, y-, and z-axis is adjusted automatically according to the range of data units in the x, y, and z directions.

How do I limit axis in Seaborn?

You can control the limits of X and Y axis of your plots using matplotlib function plt. xlim() and plt. ylim() . In this example, lmplot() function of seaborn is used to plot a basic scatterplot with iris dataset.


2 Answers

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.

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) 

Figure produced by script

like image 104
esmit Avatar answered Oct 17 '22 23:10

esmit


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? 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. 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.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.

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.

like image 26
jez Avatar answered Oct 17 '22 23:10

jez