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?
Related: Force xlim
, ylim
, and axes('equal')
at the same time by letting margins auto-adjust
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.
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] .
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With