Search code examples
pythonmatplotlibaxis

Excluding a label on the y-axis in Matplotlib: How?


I would not like to have the first label on the y-axis on my figure which looks like this Figure now, since I don't find it very nice to have it like this.

In my solution, I tried excluding the corresponding (first) tick, however this is not necessary. I just want the label (i.e., 0.3) to diminish.

My try for achieving this goal was the following addition to the script:

y_ticks = ax.get_yticks() 
ax.set_yticks(y_ticks[1:])

But I could not solve the problem.

The complete part regarding plotting looks like this

plt.style.use('seaborn-v0_8-bright')
plt.rcParams.update({'font.size': 11, 'font.family': 'serif'})

fig, ax = plt.subplots(figsize=(10, 6))

ax.plot(specific_alpha_degrees, best_X_values, linestyle='-', marker='', label=r'$P_1$', color='blue')
ax.plot(specific_alpha_degrees, best_X_values2, linestyle='--', marker='', label=r'$P_2$', color='green')
ax.plot(specific_alpha_degrees, best_X_values3, linestyle='-.', marker='', label=r'$P_3$', color='red')
ax.plot(specific_alpha_degrees, best_X_values4, linestyle=':', marker='', label=r'$P_4$', color='purple')
ax.plot(specific_alpha_degrees, best_X_values5, linestyle='-', marker='', label=r'$P_5$', color='orange')


y_ticks = ax.get_yticks() 
ax.set_yticks(y_ticks[1:])


ax.set_xlabel(r'Incline [$deg$]')
ax.set_ylabel(r'$x_{\text{opt}}$')
ax.set_xlim(min(specific_alpha_degrees)-1, max(specific_alpha_degrees)+1)
ax.grid(True, which='major', linestyle='--', linewidth=0.5)
ax.minorticks_on()

ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize='medium', frameon=False, framealpha=0.9, borderpad=1)

ax.spines['left'].set_position(('data', 0))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_color('black')
ax.spines['left'].set_color('black')

fig.tight_layout()  

plt.show()

Solution

  • I propose a two-step solution

    1. remove the first y tick label
    2. use an annotation to replace the missing label, specifying a few property of a fancy arrow to my taste, you should modify those details to suit yours.

    image

    import matplotlib.pyplot as plt
    
    # your code START
    fig, ax = plt.subplots()
    plt.plot((-10,10), (0.3, 0.98))
    
    ax.set_ylim((0.3, 1))
    ax.grid(True, which='major', linestyle='--', linewidth=0.5)
    ax.minorticks_on()
    ax.spines['left'].set_position(('data', 0))
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_color('black')
    ax.spines['left'].set_color('black')
    fig.tight_layout()
    # your code END
    
    # the sillyness below avoids a warning, 
    ax.set_yticks(ax.get_yticks())
    
    # set the 1s y tick label to ''
    ax.set_yticklabels(['']+ax.get_yticklabels()[1:])
    
    # use an :annotate: to show the _equivalent_ y tick label
    dx, dy = 3, 0.04
    ax.annotate(
        '0.3', (0+dx/10, 0.3+dy/7), (0+dx, 0.3+dy), va='bottom',
        arrowprops=dict(
            arrowstyle='->',
            linewidth=0.5,
            relpos=(0., 0.5), 
            connectionstyle="arc3,rad=0.2"
        )      
    plt.show()