title: How to automatically set the y-axis limits after limiting the x-axis
body: <p>Let's say that I have a certain number of data sets that I want to plot together.</p>
  <p>And then I want to zoom on a certain part (for example, using <code>ax.set_xlim</code>, or <code>plt.xlim</code> or <code>plt.axis</code>). When I do that it still keeps the calculated range prior to the zoom. How can I make it rescale to what is currently being shown?</p>
  <p>For example, using</p>
  <pre class="lang-py prettyprint-override"><code>import matplotlib
  import matplotlib.pyplot as plt
  import matplotlib.gridspec as gridspec
  
  data_x = [d for d in range(100)]
  data_y = [2*d for d in range(100)]
  data_y2 = [(d-50)*(d-50) for d in range(100)]
  
  fig = plt.figure(constrained_layout=True)
  gs = gridspec.GridSpec(2, 1, figure=fig)
  
  ax1 = fig.add_subplot(gs[0, 0])
  ax1.grid()
  ax1.set_xlabel('x')
  ax1.set_ylabel('y')
  ax1.scatter(data_x, data_y, s=0.5)
  ax1.scatter(data_x, data_y2, s=0.5)
  
  ax2 = fig.add_subplot(gs[1, 0])
  ax2.grid()
  ax2.set_xlabel('x')
  ax2.set_ylabel('y')
  ax2.scatter(data_x, data_y, s=0.5)
  ax2.scatter(data_x, data_y2, s=0.5)
  ax2.set_xlim(35,45)
  
  fig.savefig('scaling.png', dpi=300)
  plt.close(fig)
  </code></pre>
  <p>Which generate</p>
  <p><a href="https://i.stack.imgur.com/P9XjE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P9XjE.png" alt="failed zoom" /></a></p>
  <p>as you can see the plot below gets hard to see something since the y-axis kept using the same range as the non-limited version.</p>
  <p>I have tried using <code>relim</code>, <code>autoscale</code> or <code>autoscale_view</code> but that did not work. For a single data set, I could use <code>ylim</code> with the minimum and maximum values for that dataset. But for different data set, I would have to look through all of them.</p>
  <p>Is there a better way to force a recalculation of the y-axis range?</p>