Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib Title and Label Functions

Matplotlib Title and Label Functions


Matplotlib Reference Documentation Matplotlib Reference Documentation

Matplotlib provides a set of functions for setting chart titles, axis labels, and the overall title, which are fundamental to making a chart complete and readable.

Functions involved

function Function Corresponding Axes method
title() Set the Axes title ax.set_title()
xlabel() / ylabel() Set the x/y axis labels ax.set_xlabel() / ax.set_ylabel()
suptitle() Set the overall Figure title fig.suptitle()
supxlabel() / supylabel() Set Figure-level x/y axis labels fig.supxlabel() / fig.supylabel()

Function definition

matplotlib.pyplot.title(label, fontdict=None, loc=None, pad=None,
    *, y=None, **kwargs)
matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *,
    loc=None, **kwargs)
matplotlib.pyplot.ylabel(ylabel, fontdict=None, labelpad=None, *,
    loc=None, **kwargs)
matplotlib.pyplot.suptitle(t, **kwargs)
matplotlib.pyplot.supxlabel(t, **kwargs)
matplotlib.pyplot.supylabel(t, **kwargs)

Common parameters

Parameters Description
label / xlabel / ylabel / t Text content (required)
fontsize / size Font size, such as 14, 'large', 'x-large'
fontweight / weight Font weight: 'normal', 'bold', 'light'
color Text Colors
fontdict Font properties dictionary, such as {'fontsize':14, 'color':'blue'}
loc Alignment position: 'center' (default), 'left', 'right'
pad / labelpad Spacing between the text and the axes/chart edge (in points)
y (specific to title) Vertical position of the title (Axes coordinates, 1.0 is the top)

Axes The method naming of the object starts with set_ , such as ax.set_title()、ax.set_xlabel(), which is the recommended style for the object-oriented interface.


Usage examples

Example 1: Basic titles and labels

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

x

=

np.

linspace

(

0

,

10

,

100

)

y

=

np.

sin

(

x

)

exp

(

-x/

5

)

fig

,

ax

=

plt.

subplots

(

figsize

=

(

8

,

4

)

,

layout

=

'constrained'

)

ax.

plot

(

x

,

y

,

'steelblue'

,

linewidth

=

2

)

Set the title (Axes level)

ax.

set_title

(

'Damped Sine Wave'

,

fontsize

=

14

,

fontweight

=

'bold'

)

Set the axis labels

ax.

set_xlabel

(

'Time (seconds)'

,

fontsize

=

12

)

ax.

set_ylabel

(

'Amplitude (V)'

,

fontsize

=

12

)

ax.

grid

(

True

,

alpha

=

0.3

)

plt.

show

(

)

Example 2: Figure overall title + axis labels

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

x

=

np.

linspace

(

0

,

10

,

100

)

fig

,

axes

=

plt.

subplots

(

2

,

2

,

figsize

=

(

10

,

8

)

,

                          layout

=

'constrained'

)

Each subplot has its own title

axes

[

0

,

0

]

.

plot

(

x

,

np.

sin

(

x

)

)

;

axes

[

0

,

0

]

.

set_title

(

'sin(x)'

)

axes

[

0

,

1

]

.

plot

(

x

,

np.

cos

(

x

)

)

;

axes

[

0

,

1

]

.

set_title

(

'cos(x)'

)

axes

[

1

,

0

]

.

plot

(

x

,

np.

sin

(

2

*x

)

)

;

axes

[

1

,

0

]

.

set_title

(

'sin(2x)'

)

axes

[

1

,

1

]

.

plot

(

x

,

np.

cos

(

2

*x

)

)

;

axes

[

1

,

1

]

.

set_title

(

'cos(2x)'

)

Figure-level overall title

fig.

suptitle

(

'Trigonometric Functions Overview'

,

             fontsize

=

16

,

fontweight

=

'bold'

,

y

=

1.02

)

Figure-level axis labels (centered across all subplots)

fig.

supxlabel

(

'x (radians)'

,

fontsize

=

13

)

fig.

supylabel

(

'Amplitude'

,

fontsize

=

13

)

plt.

show

(

)

Example 3: Title position and style

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

x

=

np.

linspace

(

0

,

10

,

100

)

fig

,

(

ax1

,

ax2

)

=

plt.

subplots

(

1

,

2

,

figsize

=

(

10

,

4

)

,

                                 layout

=

'constrained'

)

Left: left-aligned title

ax1.

plot

(

x

,

np.

sin

(

x

)

,

'steelblue'

)

ax1.

set_title

(

'Left-aligned Title'

,

              loc

=

'left'

,

               

Left align

              fontsize

=

12

,

              color

=

'steelblue'

)

ax1.

set_xlabel

(

'X-axis'

,

fontsize

=

10

,

color

=

'gray'

)

Right: right-aligned title

ax2.

plot

(

x

,

np.

cos

(

x

)

,

'coral'

)

ax2.

set_title

(

'Right-aligned Title'

,

              loc

=

'right'

,

             

Align right

              fontsize

=

12

,

              color

=

'coral'

,

              fontstyle

=

'italic'

)

ax2.

set_ylabel

(

'Y-axis'

,

fontsize

=

10

,

color

=

'gray'

,

               rotation

=

0

,

labelpad

=

20

)

 

Labels are not rotated

plt.

show

(

)

Example 4: LaTeX math formula titles

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

x

=

np.

linspace

(

5

,

5

,

200

)

fig

,

(

ax1

,

ax2

)

=

plt.

subplots

(

1

,

2

,

figsize

=

(

10

,

4

)

,

                                 layout

=

'constrained'

)

Gaussian distribution

sigma

,

mu

=

1.0

,

0.0

y1

=

1

/

(

sigma * np.

sqrt

(

2

*np.

pi

)

)

exp

(

(

x-mu

)

**

2

/

(

2

sigma*

2

)

)

ax1.

plot

(

x

,

y1

,

'steelblue'

,

linewidth

=

2

)

Use a LaTeX formula as the title

ax1.

set_title

(

    r

'$f(x) = frac{1}{sigmasqrt{2pi}}'

    r

'expleft(-frac{(x-mu)^2}{2sigma^2}right)$'

,

    fontsize

=

12

)

Polynomial

y2

=

x**

3

3

*x

ax2.

plot

(

x

,

y2

,

'coral'

,

linewidth

=

2

)

ax2.

set_title

(

r

'$f(x) = x^3 - 3x$'

,

fontsize

=

14

)

ax2.

axhline

(

y

=

0

,

color

=

'gray'

,

linewidth

=

0.5

)

for

ax

in

[

ax1

,

ax2

]

:

    ax.

set_xlabel

(

'x'

)

    ax.

grid

(

True

,

alpha

=

0.3

)

fig.

suptitle

(

'Functions with LaTeX Titles'

,

fontsize

=

14

,

             fontweight

=

'bold'

)

plt.

show

(

)

print

(

"runoob: LaTeX titles displayed"

)


Common questions

The difference between title() and suptitle()?

title()(or ax.set_title()): sets a title above a single Axes.

suptitle()(or fig.suptitle()): sets an overall title at the top of the entire Figure, covering all subplots.

How to make titles support Chinese?

Set the font:plt.rcParams['font.sans-serif'] = ['SimHei'] or plt.rcParams['font.family'] = 'sans-serif'。

Matplotlib Reference Documentation Matplotlib Reference Documentation