title: Matplotlib: center Y-axis on 0
body: <p>I've made a function to graph economic performance, but the output is often lopsided on the y-axis.</p>
  <p>The below graph shows the problem. The range of y values makes the chart default to the max/min as the range of the y axis.</p>
  <p><a href="https://i.stack.imgur.com/Ch8dU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ch8dU.png" alt="enter image description here" /></a></p>
  <p>Is there any way to force the chart to center itself on 0, or do I need derive the max and min y values within the function?</p>
  <p>The function is below. If you'd like me to replace the variables with values to repro the chart lmk- it's a bit of a task.</p>
  <pre><code>def recession_comparison(key, variable, dimension):
      '''
      Creates the &quot;scary chart&quot;- proportional growth for a single area/industry. All recessions included in chart.
  
          Parameters: 
              key (str or int): area-fips or industry_code
              variable (str): determines what economic indicator will be used in the timeline. Must be one of ['month3_emplvl' (employment), 'avg_wkly_wage' (wages), 'qtrly_estabs_count'(firms)]
              dimension (str): dimension of data to chart.
              
          Returns: 
              fig (matplotlib plot)
      '''
      fig, ax = plt.subplots(figsize =(15, 10))
      if dimension == 'area':
          index = 'area_fips'
          title = 'Recession Comparison, ' + area_titles[key] + &quot; (&quot; + str(key) + &quot;)&quot; 
      elif dimension == 'industry':
          index = 'industry_code'
          title = 'Recession Comparison: ' + industry_titles[key] + &quot; (&quot; + str(key) + &quot;)&quot; 
      for recession in recessions_int.keys():
          if recession == 'full':
              break
          loadpath = filepath(variable = variable, dimension = dimension, charttype = 'proportional', recession = recession, filetype = 'json')
          df = pd.read_json(loadpath)
          df.set_index(index, inplace = True)
          ax.plot(df.loc[key][1:-1]*100, label = str(recession), linewidth = 1.5, alpha = 0.8)
      ax.axvline(x = 6, color = 'black', linewidth = 0.8, alpha = 0.5, ls = ':', label = 'Event Quarter')
      ax.axhline(y = 0, color = 'black', linewidth = 0.8, alpha = 0.5, ls = '--', label = 'Pre-Recession baseline')
      ax.set_xlabel('Quarters since start of recession')
      ax.set_ylabel('Growth: ' + var_display[variable])
      ax.set_title(title)
      ax.yaxis.set_major_formatter(mtick.PercentFormatter())
      plt.legend()
      plt.show()
      return fig
  </code></pre>
  <p>edit: full code solution from DapperDuck:</p>
  <pre><code>def recession_comparison(key, variable, dimension):
      fig, ax = plt.subplots(figsize =(15, 10))
      if dimension == 'area':
          index = 'area_fips'
          title = 'Recession Comparison, ' + area_titles[key] + &quot; (&quot; + str(key) + &quot;)&quot; 
      elif dimension == 'industry':
          index = 'industry_code'
          title = 'Recession Comparison: ' + industry_titles[key] + &quot; (&quot; + str(key) + &quot;)&quot; 
      for recession in recessions_int.keys():
          if recession == 'full':
              break
          loadpath = filepath(variable = variable, dimension = dimension, charttype = 'proportional', recession = recession, filetype = 'json')
          df = pd.read_json(loadpath)
          df.set_index(index, inplace = True)
          ax.plot(df.loc[key][1:-1]*100, label = str(recession), linewidth = 1.5, alpha = 0.8)
      ax.axvline(x = 6, color = 'black', linewidth = 0.8, alpha = 0.5, ls = ':', label = 'Event Quarter')
      ax.axhline(y = 0, color = 'black', linewidth = 0.8, alpha = 0.5, ls = '--', label = 'Pre-Recession baseline')
      yabs_max = abs(max(ax.get_ylim(), key=abs))
      ax.set_ylim(ymin=-yabs_max, ymax=yabs_max)
      ax.set_xlabel('Quarters since start of recession')
      ax.set_ylabel('Growth: ' + var_display[variable])
      ax.set_title(title)
      ax.yaxis.set_major_formatter(mtick.PercentFormatter())
      plt.legend()
      plt.show()
      return fig
  </code></pre>
  <p>Corrected image:
  <a href="https://i.stack.imgur.com/aIkJH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aIkJH.png" alt="enter image description here" /></a></p>