TUTORIAL
Matplotlib Tutorial - Axis Label
This tutorial introduces Matplotlib axis labels like xlabel and ylabel.
On this page
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.
Parameters
| 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)
plt.show()
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.