title: How to create equal number of primary and secondary y-axes ticks with matplotlib?
body: <p>I have been working for a while to create a plot with secondary axis so that both the primary and secondary axes have equal number of major ticks so that the grid lines coincide. In the figure below I have shown grid lines on the secondary axis to illustrate the problem.</p>
  
  <p><img src="https://i.stack.imgur.com/DLy30.png" alt="enter image description here"></p>
  
  <p>By manually setting the secondary axis limits I got this plot, which is my desired output:</p>
  
  <p><img src="https://i.stack.imgur.com/audjr.png" alt="enter image description here"></p>
  
  <p>I have included the reproducible code:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  data = np.loadtxt('data.dat', skiprows=2, delimiter=',', unpack=True).transpose()
  time = data[:,0]
  pressure = data[:,1]
  lift = data[:,2]
  figure_pressure_trace = plt.figure(figsize=(5.15, 5.15))
  figure_pressure_trace.clf()
  P_vs_t = plt.subplot(111)
  P_vs_t.plot(time, pressure, linewidth=1.0)
  P_vs_t.set_ylabel(r'\textit{Pressure (bar)}', labelpad=6)
  P_vs_t.set_xlabel(r'\textit{Time (ms)}', labelpad=6)
  lift_vs_t = P_vs_t.twinx()
  lift_vs_t.plot(time, lift, color='#4DAF4A')
  lift_vs_t.set_ylabel(r'\textit{Lift(mm)}', labelpad=6)
  plt.show()
  plt.close()
  </code></pre>
  
  <p>The data is available <a href="https://drive.google.com/file/d/0B4oAWIdgwj-eNGQ5dmtSQ2hLT0k/view?usp=sharing" rel="nofollow noreferrer">here</a>.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I created a function to create equal number of ticks, the entire code is:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  def equal_y_ticks(primary, secondary):
      y_min_primary, y_max_primary = primary.get_ybound()
      y_min_secondary, y_max_secondary = secondary.get_ybound()
      primary_ticks = len(primary.yaxis.get_major_ticks())
      secondary_ticks = len(secondary.yaxis.get_major_ticks())
      primary_spacing = (y_max_primary - y_min_primary) / (primary_ticks - 1)
      secondary_spacing = (y_max_secondary - y_min_secondary) / (secondary_ticks - 1)
      ticks = max(primary_ticks, secondary_ticks)
      if secondary_ticks &lt; primary_ticks:
          y_max_secondary = y_min_secondary + (primary_ticks * secondary_spacing)
          secondary.yaxis.set_ticks(np.arange(y_min_secondary, y_max_secondary, secondary_spacing))
      else:
          y_max_primary = y_min_primary + (secondary_ticks * primary_spacing)
          primary.yaxis.set_ticks(np.arange(y_min_primary, y_max_primary, primary_spacing))
  
  data = np.loadtxt('data.dat', skiprows=2, delimiter=',', unpack=True).transpose()
  time = data[:,0]
  pressure = data[:,1]
  lift = data[:,2]
  figure_pressure_trace = plt.figure(figsize=(5.15, 5.15))
  figure_pressure_trace.clf()
  P_vs_t = plt.subplot(111)
  P_vs_t.plot(time, pressure, linewidth=1.0)
  P_vs_t.set_ylabel(r'\textit{Pressure (bar)}', labelpad=6)
  P_vs_t.set_xlabel(r'\textit{Time (ms)}', labelpad=6)
  lift_vs_t = P_vs_t.twinx()
  lift_vs_t.plot(time, lift, color='#4DAF4A')
  equal_y_ticks(P_vs_t, lift_vs_t)
  lift_vs_t.set_ylabel(r'\textit{Lift(mm)}', labelpad=6)
  plt.show()
  plt.close()
  </code></pre>
  
  <p>But this function gives me plots like these (for some data):</p>
  
  <p><img src="https://i.stack.imgur.com/yg9Fs.png" alt="enter image description here"></p>