Library matplotlib

[matplotlib]How to Add a Title[Position, Size, Font]

This article shows how to add a title in matplotlib.

Contents

  • How to add a title
  • How to adjust title position
  • How to change title size
  • How to change title font

sponsored link

How to add a title

In the beginning, I show how to add a title.

There are the two graph drawing methods in matplotlib.

Each method has a slightly different function for adding a title.

【 pyplot.~~~ 】

The "pyplot module" is often imported in the form "import matplotlib.pyplot as plt", so it is often written as "plt.~~~" in the code.

"matplotlib" is made to imitate MATLAB's method of generating plots, which is called "pyplot".

The way using "pyplot" can easily create graphs, but does not allow for fine tuning, so it is suitable when you want to check results quickly.

You can use the "plt.title("string")" functions to add a title to a graph.

This is the example code.

import numpy as np
import matplotlib.pyplot as plt

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

plt.plot(x, y)

plt.title('Title')

plt.show()

【 ax.~~~ 】

This is an object-oriented method.

First, create an instance of the "Figure" object.

A single "Figure" can contain multiple subplots, called "Axes".

"Figure" and "Axes" objects are created by using "plot.subplots( )" command.

Draw graphs for individual "Axes" objects using "ax.plot( )".

You can use the "ax.set_title("string")" functions to add a title to a graph.

This is the example code.

import numpy as np
import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_title('Title')

plt.show()

The above code generates the following graph.

The same graph is output whether "pyplot.~~~" or "ax.~~~" is used.

Please also see this article.

Difference between "plt" plot and "ax" plot

In subsequent examples, I will use "ax.~~~" to create a graph.

sponsored link

How to adjust title position

I show how to adjust title position.

You can adjust title position by using "loc=" argument.

This is the example code.

import numpy as np
import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_title('Title_left', loc='left')
ax.set_title('Title_center', loc='center')
ax.set_title('Title_right', loc='right')


plt.show()

The above code generates the following graph.

> ax.set_title('Title_left', loc='left')
> ax.set_title('Title_center', loc='center')
> ax.set_title('Title_right', loc='right')

The argument "loc=" is used to adjust the title position.

How to change title size

I show how to change title size.

You can change title size by using "fontsize=" argument.

This is the example code.

import numpy as np
import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_title('Title', fontsize=30)

plt.show()

The above code generates the following graph.

sponsored link

How to change title font

I show how to change title font.

You can change title font by using "fontname=" argument.

This is the example code.

import numpy as np
import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_title('Title', fontsize=30, fontname='Cambria')

plt.show()

The above code generates the following graph.

The following article explains that the list of fonts available in matplotlib.

List of available Fonts

sponsored link

-Library, matplotlib
-,