Matplotlib set_yticklabels – Helpful Guide

In this Python Matplotlib tutorial, we will discuss Matplotlib set_yticklabels in matplotlib. Here we will cover different examples related to set_yticklabels using matplotlib. And we will also cover the following topics:

  • Matplotlib set_yticklabels
  • Matplotlib set_yticklabels fontdict
  • Matplotlib set_yticklabels fontsize
  • Matplotlib set_yticklabels fontstyle
  • Matplotlib set_yticklabels color
  • Matplotlib set_yticklabels vertical alignment
  • Matplotlib set_yticklabels horizontal alignment
  • Matplotlib set_yticklabels rotation
  • Matplotlib set_yticklabels invisible
  • Matplotlib set_yticklabels alignement
  • Matplotlib set_yticklabels minor
  • Matplotlib colorbar set_yticklabels

Matplotlib set_yticklabels

In this section, we learn about the set_yticklabels() function in the axes module of matplotlib in Python. The set_yticklabels function is used to set the y-ticks labels with the list of string labels.

The syntax is given below:

matplotlib.axes.Axes.set_yticklabels(labels, fontdict=None, minor=False, **kwargs)

The following are the parameters:

ParametersValueDefaultDescription
labelslist of stringThis parameter is used to specify the list of string labels.
fontdictdict{ ‘fontsize’ : rcParams[ ‘axes.titlesize ‘], ‘fontweight’ : rcParams[ ‘axes.titleweight ‘], ‘verticalalignment’ : ‘baseline’, ‘horizontalalignment’ : loc}This parameter is used to control the appearance of the ticklabels.
minorboolFalseSpecify whether to set minor ticklabels or not.
Parameters

Warning

This method only be used after fixing the tick positions using Axes.set_yticks.

Let’s see an example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.linspace(0, 5 * np.pi, 100)
y = np.sin(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1, 2, 3])
ax.set_yticklabels(['Label-1', 'Label-2', 'Label-3', 'Label-4', 'Label-5'])
 
# Add fig title

fig.suptitle('set_yticklabels()function Example', fontweight ="bold")

# Display

plt.show()
  • In the above example, we import numpy and matplotlib.pyplot library.
  • After this, we create a subplot by using subplots() function.
  • To define data coordinates, we use linespace() and sin() function.
  • To plot the graph between x and y data coordinates, we use the plot() function.
  • To fix the position of ticks at the y-axis, use the set_yticks() function.
  • To set the string labels at the y-axis, use the set_yticklabels() functions.
  • We use the suptitle() function to add supltilte on the figure
  • To display the plot on the user’s screen, use the show() function.
matplotlib set_yticklabels
set_yticklabels()

Read: Put legend outside plot matplotlib

Matplotlib set_yticklabels fontdict

We’ll learn how to use the fontdict parameter of the set_yticklabels method. fontdict parameter is a dictionary that is used to control the appearance of the ticklabels.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels(labels,fontdict=None) 

Let’s see an example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.linspace(0, 5 * np.pi, 150)
y = np.sin(x)
    
# Plot

ax.plot(x, y)

# font dict

font_dict = {'fontsize': 15, 'fontweight': 'bold', 
             'verticalalignment':'top'}

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Label-1', 'Label-2', 'Label-3'], fontdict=font_dict)
 
# Add fig title

fig.suptitle('set_yticklabels fontdict Example', fontweight ="bold")

# Display

plt.show()

Here we pass the fontdict parameter to the set_yticklabels method. We create a dictionary font_dict to change the appearance of tick labels with the following key and value:

keyvalue
fontsize15
fontweightbold
verticalalignmenttop

Output:

matplotlib set_yticklabels fontdict
set_yticklabels(fontdict=None)

Read: Matplotlib invert y axis

Matplotlib set_yticklabels fontsize

Here we’ll learn how to change the font size of y-axis ticklabels. To change the size of the font, we pass the fontsize argument to the set_yticklabels method.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels(labels, fontsize=None)

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot


fig, ax = plt.subplots()
    
# Define Data

x = np.linspace(0, 5 * np.pi, 150)
y = np.cos(60*x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Label-1', 'Label-2', 'Label-3'],fontsize=20 )
 
# Add fig title

fig.suptitle('set_yticklabels fontsize Example', fontweight ="bold")

# Display

plt.show()

In the above example, we set text labels at the y-axis by using the set_yticklabels method, and the fontsize argument is passed to change the font size of the ticklabels.

READ:  Batch Normalization TensorFlow [10 Amazing Examples]

We assign a 20pt value to the fontsize argument.

matplotlib set_yticklabels fontsize
set_yticklabels(fontsize=None)

Read: Matplotlib title font size

Matplotlib set_yticklabels fontstyle

We’ll learn how to change the font style of the tick labels at the y-axis. To change the style we pass the fontstyle argument to the set_yticklabels method.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels(labels, fontstyle=None)

Let’s see an example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.linspace(0, 250, 250)
y = np.sin(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Label-1', 'Label-2', 'Label-3'],fontstyle='oblique')
 
# Add fig title

fig.suptitle('set_yticklabels fontstyle Example', fontweight ="bold")

# Display

plt.show()

Here we change the font style of the y-axis tick labels and set it to oblique.

matplotlib set_yticklabels fontstyle
set_yticklabels(fontstyle=’oblique’)

Read: Matplotlib default figure size

Matplotlib set_yticklabels color

Here we’ll learn to change the color of ticklabels. To change the color, we pass the color argument to the set_yticklabels method.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels (labels, color=None)

Let’s see an example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.linspace(0, 250, 250)
y = np.cos(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], color='green')
 
# Add fig title

fig.suptitle('set_yticklabels color Example', fontweight ="bold")

# Display

plt.show()
  • In the above example, we define data coordinates using linespace() and cos() methods, and to plot them we, use the plot() method.
  • After this, we use the set_yticks() method for fixing the position of ticks at the y-axis.
  • Then we use the set_yticklabels() method for setting string labels at the axis. And to change the color of ticklabels we pass a color argument to the method.
matplotlib set_yticklabels color
set_yticklabels(color=’green’)

By default, the color of the ticklabels is black, now we change it to green.

Read: Matplotlib savefig blank image

Matplotlib set_yticklabels vertical alignment

Here we’ll learn to change the vertical alignment of y- ticklabels. To change the alignment we pass the verticalalignment argument to the set_yticklabels() method.

We can also write va in place of verticalalignment.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels(labels, verticalalignment = 'center' | 'top' | 'bottom' | 'baseline' | 'center_baseline')

5 available different vertical alignments are:

  • center
  • top
  • bottom
  • baseline
  • center_baseline

verticalalignment=’center’

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot


fig, ax = plt.subplots()
    
# Define Data

x = np.arange(0, 20, 0.2)
y = np.cos(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], verticalalignment='center')
 
# Add fig title

fig.suptitle('set_yticklabels va=center Example', fontweight ="bold")

# Display

plt.show()
  • Here we use the arange() and cos() methods to define data coordinates.
  • After this, we use the plot() method to plot a graph between x and y coordinates.
  • To set the tick marks, use set_yticks() method.
  • To set the tick labels in string format, we use the set_yticklabels() method.
  • Here we set the verticalalignemnt of tick labels to the center.
matplotlib set_yticklabels verticalalignment
verticalalignment = ‘center’

verticalalignment=’top’

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.arange(0, 20, 0.2)
y = np.cos(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], verticalalignment='top')
 
# Add fig title

fig.suptitle('set_yticklabels va=top Example', fontweight ="bold")

# Display

plt.show()

Here we pass the verticalalignment argument to the set_yticklabels method, to set the alignment of ticklabels to the top.

matplotlib set_yticklabels with verticalalignment
va=’top’

verticalalignment=’bottom’

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot


fig, ax = plt.subplots()
    
# Define Data

x = np.arange(0, 20, 0.2)
y = np.cos(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], verticalalignment='bottom')
 
# Add fig title

fig.suptitle('set_yticklabels va=bottom Example', fontweight ="bold")

# Display

plt.show()
matplotlib set_yticklabels with va
va=bottom

verticalalignment=’baseline’

Code:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data


x = np.arange(0, 20, 0.2)
y = np.cos(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], verticalalignment='baseline')
 
# Add fig title

fig.suptitle('set_yticklabels va=baseline Example', fontweight ="bold")

# Display

plt.show()

Here we set the vertical alignment of ticklabels to baseline.

matplotlib set_yticklabels va
verticalalignment=’baseline’

verticalalignment=’center_baseline’

Code:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot


fig, ax = plt.subplots()
    
# Define Data

x = np.arange(0, 20, 0.2)
y = np.cos(x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], verticalalignment='center_baseline')
 
# Add fig title

fig.suptitle('set_yticklabels va=center_baseline Example', fontweight ="bold")

# Display

plt.show()
matplotlib set_yticklabels having va
verticalalignment=’center_baseline’

Read: Matplotlib bar chart labels

READ:  How to delete session property in Django

Matplotlib set_yticklabels horizontal alignment

Here we’ll learn to change the horizontal alignment of y- ticklabels. To change the alignment pass horizontalalignment argument to set_yticklabels() method.

You can write ha in place of horizontalalignment.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels(labels, horizontalalignment= 'center' | 'right' | 'left'

3 different horizontal alignments are:

  • center
  • right
  • left

horizontalalignment=’center’

Code:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.arange(10, 100, 0.25)
y = np.sin(30*x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], horizontalalignment = 'center')
 
# Add fig title

fig.suptitle('set_yticklabels ha=center Example', fontweight ="bold")

# Display

plt.show()
  • Here we use the arange() and sin() methods to define data coordinates.
  • After this, we use the plot() method to plot a graph between x and y coordinates.
  • To set the tick marks, use set_yticks() method.
  • To set the tick labels in string format, we use the set_yticklabels() method.
  • Here we set the horizontalalignment of tick labels to the center.
matplotlib set_yticklabels horizontalalignment
horizontalalignment=’center’

horizontalalignment=’right’

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.arange(10, 100, 0.25)
y = np.sin(30*x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], horizontalalignment = 'right')
 
# Add fig title

fig.suptitle('set_yticklabels ha=right Example', fontweight ="bold")

# Display

plt.show()
matplotlib set_yticklabels ha
set_yticklabels(horizontalalignemnt=’right’)

horizontalalignment=’left’

Here we are going to set the horizontal alignment of y-axis ticklabels to left.

Example:

# Import Library


import numpy as np
import matplotlib.pyplot as plt

# Create subplot


fig, ax = plt.subplots()
    
# Define Data


x = np.arange(10, 100, 0.25)
y = np.sin(30*x)

# Plot

ax.plot(x, y)

# Set ticklabels


ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'], horizontalalignment = 'left', fontsize=20)
 
# Add fig title


fig.suptitle('set_yticklabels ha=left Example', fontweight ="bold")

# Display


plt.show()

Output:

matplotlib set_yticklabels having horizontalalignment
ha=’left’

Read: Matplotlib plot error bars

Matplotlib set_yticklabels rotation

Here we’ll learn to rotate the y-axis ticklabels. To change the angle of rotation we pass the rotation argument to the set_yticklabels() method. Bascially, it is used to avoid overlapping of the labels.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels(labels , rotation=None)

Let’s see an example:

# Import Library


import numpy as np
import matplotlib.pyplot as plt

# Create subplot


fig, ax = plt.subplots()
    
# Define Data


x = np.arange(10, 100, 30)
y = np.sin(90*x)
    
# Plot


ax.plot(x, y)

# Set ticklabels


ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Value-1', 'Value-2', 'Value-3'],rotation=45)
 
# Add fig title


fig.suptitle('set_yticklabels rotation Example', fontweight ="bold")

# Display


plt.show()
  • In the above example, we plot a graph by using the plot() method. And to define the data coordinates we are using the arange() and sin() method of numpy.
  • To rotate the ticklabels here we use the set_yticklabels() method with rotation argument.
matplotlib set_yticklabels rotation
set_yticklabels(rotation=45)

Read: Draw vertical line matplotlib

READ:  How to count duplicates in Pandas dataframe in Python [4 Methods]

Matplotlib set_yticklabels invisible

Here we’ll learn how to invisible ticklabels at the y-axis. Here we set the labels to be empty so that it makes the axis text hide. But the ticks remain visible.

The following is the syntax:

matplotlib.axes.Axes.set_yticklabels([])

Let’s have a look at an example related to invisible labels:

# Import Library


import numpy as np
import matplotlib.pyplot as plt

# Create subplot


fig, ax = plt.subplots()
    
# Define Data

x = np.linspace(0, 5 * np.pi, 150)
y = np.cos(60*x)
    
# Plot

ax.plot(x, y)

# Set ticklabels

ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels([] )
 
# Add fig title

fig.suptitle('set_yticklabels fontsize Example', fontweight ="bold")

# Display

plt.show()
matplotlib set_yticklabels invisible
Invisible Tick Labels

Here you see that if we pass the blank list to the set_yticklabels() method, the labels get invisible but ticks remain there.

Read: Horizontal line matplotlib

Matplotlib set_yticklabels alignemnt

Here we’ll learn to change the alignment of the y-axis ticklabels. To change the alignment pass rotation as an argument to set_yticklabels method and set’s its value to vertical or horizontal.

By default, the alignment of y-axis labels is horizontal.

The following is the syntax:

matplotlib.axes.Axes.set_ticklabels(labels, rotation= 'vertical' | 'horizontal')

Example:

# Import Library


import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()
    
# Define Data

x = np.linspace(0, 5 * np.pi, 150)
y = np.cos(90*x)
    
# Plot

ax.plot(x, y)

# Set ticklabels


ax.set_yticks([-1 , 0, 1])
ax.set_yticklabels(['Lable-1', 'Label-2', 'Label-3'], rotation='vertical')
 
# Add fig title


fig.suptitle('set_yticklabels invisible Example', fontweight ="bold")

# Display


plt.show()

In the above example, we pass the rotation argument to the set_yticklabels() method and set its value to vertical, to get vertical-align labels.

matplotlib set_yticklabels alignemnt
set_yticklabels(rotation=’vertical’)

Read: Stacked Bar Chart Matplotlib

Matplotlib set_yticklabels minor

Here we’ll learn how to set the minor ticklabels rather than the major ones at the y-axis.

The syntax is given below:

matplotlib.axes.Axes.set_yticklabels(labels, minor=True)

Code:

# Import Library

import matplotlib.pyplot as plt

# Create subplots
  
fig, ax = plt.subplots()

# Plot graph

ax.plot(range(12, 24), range(12))

# Set tick marks

ax.set_yticks((1, 3, 5, 7, 9), minor=True)

# Set ticklabels

ax.set_yticklabels(("Label-1", "Label-2",
                    "Label-3", "Label-4", "Label-5"), 
                     minor=True, color='Red')

# Add fig title

fig.suptitle('set_yticklabels minor Example', fontweight ="bold")

# Display
             
plt.show()

In the above example, we pass minor as an argument to both the methods i.e. set_yticklabels and set_yticks to set the minor ticklabels and ticks instead of major ones.

matplotlib set_yticklabels minor
set_yticklabels(minor=True)

Read: Matplotlib two y axes

Matplotlib colorbar set_yticklabels

Here we’ll learn how to set text labels at colorbar axes.

Example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import randn

# Subplots

fig, ax = plt.subplots()

# Fix random

np.random.seed(19680801)

# Define Data and Plot

data = np.clip(randn(250, 300), -1, 1)
cax = ax.imshow(data, cmap=cm.cividis)

# Add colorbar and set ticks and ticklabel

cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['Low', 'Medium', 'High']) 

# Add fig title

fig.suptitle('set_yticklabels colorbar Example', fontweight ="bold")


# Display

plt.show()
  • Here we fix random number by using random.seed() method of numpy.
  • Then we define data using the clip() method of numpy.
  • To plot the data we use, imshow() method.
  • By using colorbar() method, we add a colorbar to a plot and set ticks.
  • Then we use the set_yticklabels() method to set the y-axis labels with the list of string labels.
matplotlib colorbar set_yticklabels
set_yticklabels()

Also, check the following related posts.

So, in this Python tutorial, we have discussed the “Matplotlib set_yticklabels” and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.

  • Matplotlib set_yticklabels
  • Matplotlib set_yticklabels fontdict
  • Matplotlib set_yticklabels fontsize
  • Matplotlib set_yticklabels fontstyle
  • Matplotlib set_yticklabels color
  • Matplotlib set_yticklabels vertical alignment
  • Matplotlib set_yticklabels horizontal alignment
  • Matplotlib set_yticklabels rotation
  • Matplotlib set_yticklabels invisible
  • Matplotlib set_yticklabels alignement
  • Matplotlib set_yticklabels minor
  • Matplotlib colorbar set_yticklabels