Silvia
Silvia

Reputation: 133

major ticks with locator_params

I cannot display the major ticks of the 1st, 2nd, 3rd y-axis at the same level. If you have a look at the figure below one can see that:

I had found topics here that suggest to use "locator_parameters":

    plt.locator_params(axis='y', nbins=10) 

but I haven't manage to make it work in my case. I want to have 10 bins for all y-axis and I want the major ticks to be aligned.

enter image description here

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib import rcParams
    %matplotlib inline
    x = np.random.rand(20)
    y1 = x*5
    y2 = x*5 + 0.2
    y3 = x*x*3.5 + 0.2*x
    y4 = x*5 + 0.2*x
    yLimMin = 0
    yLimMax = 2.1
    lineWidth = 1.0
    fontSize = 24
    subTitle = ""

    plt.rcParams.update({'axes.labelsize': 'small'})
    fig = plt.figure(figsize=(21,29.7))
    ax11 = fig.add_subplot(411)

    subplotAdjustRight = 0.90
    mks = 19 # marker step
    ax11.plot(x,y1, linestyle='-', linewidth=lineWidth, color = 'k',
      marker='*', markevery=11*mks, 
      label="CO")
    ax11.set_ylabel('CO [%]', color='k')
    plt.ylim((0,5))
    fig.suptitle(subTitle, fontsize = fontSize)
    ax11.yaxis.grid()
    ax11.locator_params(axis='y', nbins=10)
    ax12=ax11.twinx()
    ax12.plot(x,y2,linestyle='-', linewidth=lineWidth, color='r',
      marker='*', markevery=11*mks, 
      label="CO22")
    ax12.set_ylabel('NO [%]', color='r')
    plt.ylim((0,13))
    ax12.locator_params(axis='y', nbins=10)

    ax13= ax11.twinx()
    rspine = ax13.spines['right']
    rspine.set_position(('axes', 1.05))
    ax13.set_frame_on(True)
    ax13.plot(x,y3,linestyle='-', linewidth=lineWidth, color='m',
      marker='*', markevery=11*mks, 
      label="CO222")
    ax13.set_ylabel('O [%] ', color='m')
    plt.ylim((0,22))
    ax13.locator_params(axis='y', nbins=10)

Upvotes: 1

Views: 520

Answers (1)

Silvia
Silvia

Reputation: 133

ax14.set_yticks(np.linspace(ax14.get_ybound()[0], ax14.get_ybound()[1], 11))

x = np.random.rand(20)
y1 = x*5
y2 = x*5 + 0.2
y3 = x*x*3.5 + 0.2*x
y4 = x*5 + 0.2*x
#from matplotlib.ticker import AutoMinorLocator
yLimMin = 0
yLimMax = 2.1
lineWidth = 1.0
fontSize = 24
subTitle = ""


plt.rcParams.update({'axes.labelsize': 'small'})
fig = plt.figure(figsize=(21,29.7))
ax11 = fig.add_subplot(411)

subplotAdjustRight = 0.90
mks = 19 # marker step
#matplotlib.locator.MAXTICKS = 5
ax11.plot(x,y1, linestyle='-', linewidth=lineWidth, color = 'k',
                     marker='*', markevery=11*mks, 
                     label="CO")
ax11.set_ylabel('CO [%]', color='k')
plt.ylim((0,5))
fig.suptitle(subTitle, fontsize = fontSize)
ax11.yaxis.grid()
#ax11.locator_params(axis='ax11', nbins=10)
ax11.set_yticks(np.linspace(ax11.get_ybound()[0], ax11.get_ybound()[1], 11))

ax12=ax11.twinx()
ax12.plot(x,y2,linestyle='-', linewidth=lineWidth, color='r',
                marker='*', markevery=11*mks, 
                label="CO22")
ax12.set_ylabel('NO [%]', color='r')
plt.ylim((0,13))
#ax12.locator_params(axis='ax11', nbins=10)
ax12.set_yticks(np.linspace(ax12.get_ybound()[0], ax12.get_ybound()[1], 11))

ax13= ax11.twinx()
rspine = ax13.spines['right']
rspine.set_position(('axes', 1.05))
ax13.set_frame_on(True)
ax13.plot(x,y3,linestyle='-', linewidth=lineWidth, color='m',
                marker='*', markevery=11*mks, 
                label="CO222")
ax13.set_ylabel('O [%] ', color='m')
plt.ylim((0,22.87))
#ax13.locator_params(axis='ax11', nbins=10)
ax13.set_yticks(np.linspace(ax13.get_ybound()[0], ax13.get_ybound()[1], 11))

ax14= ax11.twinx()
rspine = ax14.spines['right']
rspine.set_position(('axes', 1.10))
ax14.set_frame_on(True)
ax14.plot(x,y4,linestyle='-', linewidth=lineWidth, color='m',
                marker='*', markevery=11*mks, 
                label="CO2")
ax14.set_ylabel('CO [%] ', color='m')
plt.ylim((0,25))
#ax14.locator_params(axis='ax11', nbins=10)
ax14.set_yticks(np.linspace(ax14.get_ybound()[0], ax14.get_ybound()[1], 11))

Upvotes: 1

Related Questions