Matplotlib Tutorial - Axis Label

Jinku Hu Jun 25, 2020
Matplotlib Tutorial - Axis Label

In this tutorial we’re going to learn about axis labels, titles and legends in Matplotlib. These could help the graph to be self-explanatory with such kind of context.

Matplotlib Axis Label

matplotlib.pyplot.xlabel(label, fontdict=None, labelpad=None, **kwargs)

It sets the label for the x-axis. Similarly, matplotlib.pyplot.ylabel sets the label of y-axis.


Name Description
label label text
fontdict label text font dictionary, like family, color, weight and size
labelpad Spacing in points between the label and the x-axis
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 4 * np.pi, 1000)
y = np.sin(x)

plt.figure(figsize=(4, 3))

plt.plot(x, y, "r")
plt.xlabel("Time (s)", family="serif", color="r", weight="normal", size=16, labelpad=6)

Matplotlib Line Chart -Axis Label

It specifies the label of x-axis below,

plt.xlabel("Time (s)", family="serif", color="r", weight="normal", size=16, labelpad=6)

Below is the detailed explanation,

  • Times (s)

This is the label text of x-axis

  • family='serif'

It specifies the label text font family to be serif. You could choose the family from the popular options like [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]

  • color='r'

The font text has the color of red. Refer to color option in last chapter to pick up more colors.

  • weight='normal'

It specifies the label text to have a normal weight. The weight option is ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black'].

  • size=16

It assigns the font size to be 16.

  • labelpad = 6

The distance between x-axis and the label is 6 points.

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook