Nimal Naser
Nimal Naser

Reputation: 458

How to create equal number of primary and secondary y-axes ticks with matplotlib?

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.

enter image description here

By manually setting the secondary axis limits I got this plot, which is my desired output:

enter image description here

I have included the reproducible 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()

The data is available here.

UPDATE:

I created a function to create equal number of ticks, the entire code is:

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 < 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()

But this function gives me plots like these (for some data):

enter image description here

Upvotes: 4

Views: 2010

Answers (1)

tacaswell
tacaswell

Reputation: 87556

I think you are looking for LinearLocator (docs)

import matplotlib.pyplot as plt
from matplotlib import ticker as mtick
fig, ax = plt.subplots()
ax2 = ax.twinx()

ax.yaxis.set_major_locator(mtick.LinearLocator(5))
ax2.yaxis.set_major_locator(mtick.LinearLocator(5))

ax.set_ylim(0, 15)
ax2.set_ylim(0, 1500)
ax.yaxis.grid(True, lw=7, color='g', ls='--')
ax2.yaxis.grid(True, color='k', ls='-', lw=3)

Which will put N evenly spaced ticks between the min and max.

enter image description here

Upvotes: 1

Related Questions