Skip to content
Home » matplotlib Part 8 – Axis Labels and Title

matplotlib Part 8 – Axis Labels and Title

Spread the love

In the previous part of the Matplotlib series we were talking about axis ranges. In this part we’ll see how to add a title and axis labels for each axis so that we actually know what it represents.

The method used to set the title is set_title. Let’s use the example from the previous part, this time adding the title:

In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.sin(x) / x

fig, ax = plt.subplots()

ax.plot(x, y)

# Here's the title.
ax.set_title('Graph Example')
Out[1]:
Text(0.5, 1.0, 'Graph Example')

And now let’s add the axis labels using the set_xlabel and set_ylabel methods for the X and Y axes respectively:

In [2]:
fig, ax = plt.subplots()

ax.plot(x, y)

# title
ax.set_title('Graph Example')

# X label
ax.set_xlabel('x')

# Y label
ax.set_ylabel('y=sin(x)/x')
Out[2]:
Text(0, 0.5, 'y=sin(x)/x')

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply