1

I want to use ax.axis('equal') to force even spacing on X & Y, but I also want to prescribe specific ranges for the X and Y axes. If the margins are also fixed, the problem is over constrained and the result is shown on the left side of the Figure 1. If instead, the margins were allowed to automatically increase themselves to take up the slack, then xlim and ylim could stay as I set them while still satisfying axis('equal'). An example of what I'm after is shown on the right side of Figure 1. How can I allow the plot margins to "float"?

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

actual and desired output Figure 1: output from the above code snippet.

SiHa
  • 7,830
  • 13
  • 34
  • 43
EL_DON
  • 1,416
  • 1
  • 19
  • 34
  • You should put `set_xlim`, `set_ylim` to the end to avoid the automatic rescaling. – dnalow Sep 29 '16 at 17:24
  • @dnalow It doesn't work if I delete everything after `ax.set_ylim()`, and it doesn't work if I copy `set_xlim()`, `set_ylim()` to the end after everything else is done. – EL_DON Sep 29 '16 at 19:00
  • this is because of `ax.axis('equal'). It fixes the stepping, so the subplotsize has to fit your limits which means, in turn, that your figure size has to match or the subplot parameters – dnalow Sep 30 '16 at 08:39
  • Unless the margins resize themselves automatically, which is what I'm asking for. – EL_DON Sep 30 '16 at 15:11
  • Interesting and related: The `tight=True` keyword in `autoscale()` sets `xlim` or `ylim` so the plot box hugs the data. `plt.autoscale(enable=True,axis='both',tight=True)` – EL_DON Sep 30 '16 at 15:58
  • Related question: http://stackoverflow.com/questions/39730467/how-can-i-get-the-actual-axis-limits-when-using-ax-axisequal – EL_DON Oct 03 '16 at 16:09

1 Answers1

3
ax.set_aspect('equal',adjustable='box')

enter image description here

EL_DON
  • 1,416
  • 1
  • 19
  • 34