title: Define aspect ratio when using twinx
body: <p>How do I set the aspect ratio for a plot that uses <code>twinx</code>?</p>
  
  <hr>
  
  <p>Below, I provide three examples:  </p>
  
  <ol>
  <li>without <code>twinx</code> to highlight how I set the aspect ratio  </li>
  <li>with only <code>twinx</code> to illustrate how the previously defined aspect ratio is reset  </li>
  <li>my attempt to define the aspect ratio for both <code>y</code>-axis while using <code>twinx</code> (which does not work)</li>
  </ol>
  
  <p>I am using matplotlib 1.3.0<br>
  The following script illustrates how I set the aspect ratio of a simple plot:  </p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  x = np.linspace(0,1.6,50) + 50.0
  
  fig = plt.figure()
  
  ax = fig.add_subplot(111)
  
  XLIM = [50.0, 51.6]
  YLIM = [0.0, 1.1, 0.0, 11.0] 
  
  ax.plot(x,np.sin(x-50.0),'b')
  
  ax.set_xlim([XLIM[0], XLIM[1]])
  ax.set_ylim([YLIM[0], YLIM[1]])
  
  ax.set_xticks(np.arange(XLIM[0], XLIM[1], 0.2))
  ax.set_yticks(np.arange(YLIM[0], YLIM[1]+0.1, 0.1)[:-1])
  
  ax.grid(True,which='major',linestyle='solid')
  
  ax.set_aspect((XLIM[1]-XLIM[0])/(YLIM[1]-YLIM[0]))
  
  plt.show()
  </code></pre>
  
  <p>The figure this produces has a defined aspect ratio:  </p>
  
  <p><img src="https://i.stack.imgur.com/mI2cd.png" alt="first plot"></p>
  
  <p>However, if I want to add an additional <code>y</code>-axis using the <code>twinx()</code> function, the aspect ratio is reset:  </p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  x = np.linspace(0,1.6,50) + 50.0
  
  fig = plt.figure()
  
  ax = fig.add_subplot(111)
  ax2 = ax.twinx()
  
  XLIM = [50.0, 51.6]
  YLIM = [0.0, 1.1, 0.0, 11.0] 
  
  ax.plot(x,np.sin(x-50.0),'b')
  
  ax.set_xlim([XLIM[0], XLIM[1]])
  ax.set_ylim([YLIM[0], YLIM[1]])
  
  ax.set_xticks(np.arange(XLIM[0], XLIM[1], 0.2))
  ax.set_yticks(np.arange(YLIM[0], YLIM[1]+0.1, 0.1)[:-1])
  
  ax.grid(True,which='major',linestyle='solid')
  
  ax.set_aspect((XLIM[1]-XLIM[0])/(YLIM[1]-YLIM[0]))
  
  plt.show()
  </code></pre>
  
  <p>Resulting in this figure (in which the previously defined <code>y</code>-limits on the left-hand-side are reset):</p>
  
  <p><img src="https://i.stack.imgur.com/jKs0g.png" alt="first twinx attempt"></p>
  
  <p>Now, trying to specify the aspect ratio again does not yield the expected result:  </p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  x = np.linspace(0,1.6,50) + 50.0
  
  fig = plt.figure()
  
  ax = fig.add_subplot(111)
  ax2 = ax.twinx()
  
  XLIM = [50.0, 51.6]
  YLIM = [0.0, 1.1, 0.0, 11.0] 
  
  ax.plot(x,np.sin(x-50.0),'b')
  ax2.plot(x,np.cos(x-50.0)*10.,'r')
  
  ax.set_xlim([XLIM[0], XLIM[1]])
  ax.set_ylim([YLIM[0], YLIM[1]])
  ax2.set_ylim([YLIM[2], YLIM[3]])
  
  ax.set_xticks(np.arange(XLIM[0], XLIM[1], 0.2))
  ax.set_yticks(np.arange(YLIM[0], YLIM[1]+0.1, 0.1)[:-1])
  ax2.set_yticks(np.arange(YLIM[2], YLIM[3]+1.0, 1.0))
  
  ax.grid(True,which='major',linestyle='solid')
  
  ax.set_aspect((XLIM[1]-XLIM[0])/(YLIM[1]-YLIM[0]))
  ax2.set_aspect((XLIM[1]-XLIM[0])/(YLIM[3]-YLIM[2]))
  
  plt.show()
  </code></pre>
  
  <p>This produces:  </p>
  
  <p><img src="https://i.stack.imgur.com/M3NxQ.png" alt="attempt to fix the aspect ratio"></p>
  
  <p>What am I missing in order to generate a plot like the first one, but with two <code>y</code>-axis of different scales? How do I define the aspect ratio when using <code>twinx</code>?</p>