How can I get the actual axis limits when using ax.axis('equal')?
<p>I am using <code>ax.axes('equal')</code> to make the axis spacing equal on X and Y, and also setting <code>xlim</code> and <code>ylim</code>. This over-constrains the problem and the actual limits are not what I set in <code>ax.set_xlim()</code> or <code>ax.set_ylim()</code>. Using <code>ax.get_xlim()</code> just returns what I provided. How can I get the actual visible limits of the plot?</p>

<pre><code>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
</code></pre>

<p>What commands will let me draw the green box around the actual edges of the figure? </p>

<p><a href="http://i.stack.imgur.com/Z14Jg.png" rel="nofollow"><img src="http://i.stack.imgur.com/Z14Jg.png" alt="Plot showing that the results of get_xlim() and get_ylim() do not match the visible bounds of the figure"></a></p>

<p>Related: <a href="http://stackoverflow.com/questions/39775709/how-to-enforce-both-xlim-and-ylim-while-using-ax-axesequal/39795483#39795483" title="Force xlim, ylim, and axes&#40;&#39;equal&#39;&#41; at the same time by letting margins auto-adjust">Force <code>xlim</code>, <code>ylim</code>, and <code>axes('equal')</code> at the same time by letting margins auto-adjust</a> </p>

<p>The actual limits are not known until the figure is drawn.  By adding a canvas draw after setting the <code>xlim</code> and <code>ylim</code>, but before obtaining the <code>xlim</code> and <code>ylim</code>, then one can get the desired limits.</p>

<pre><code>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() #&lt;---------- 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) 
</code></pre>

<p><a href="http://i.stack.imgur.com/v4XJd.png" rel="nofollow"><img src="http://i.stack.imgur.com/v4XJd.png" alt="Figure produced by script"></a></p>

python python-2.7 matplotlib plot