Set individual font sizes for labels and titles in `pyplot` graphs

David Y.

The Problem

I’ve created a graph in Python using matplotlib.pyplot. My script looks like this:

from matplotlib import pyplot graph = pyplot.figure() pyplot.plot([1, 2, 3, 4], [4, 3, 2, 3]) pyplot.title("Productivity") pyplot.xlabel("Hours") pyplot.ylabel("Work Units") graph.savefig("workperhour.png")

I would like to set different font sizes for the figure title and axis labels. How can I do this?

The Solution

We can set properties such as font size for text in our graph by passing optional keyword arguments to the relevant pyplot functions. The matplotlib documentation provides a complete list of modifiable text properties here. In this case, we will use the fontsize property:

from matplotlib import pyplot graph = pyplot.figure() pyplot.plot([1, 2, 3, 4], [4, 3, 2, 3]) pyplot.title("Productivity", fontsize=22) pyplot.xlabel("Hours", fontsize=16) pyplot.ylabel("Work Units", fontsize=14) graph.savefig("workperhour.png")

Here, we’ve set different font sizes for each of the three text elements. We can use this same technique to specify our text color, font family, and other properties such as position and alignment.

To set the font size used for titles and axis labels globally, we must modify the relevant keys in matplotlib.rcParams. These are axes.titlesize and axes.labelsize. Note that the latter value controls the font size for both axis labels (x and y).

In the code below, we set the global font sizes for titles and axis labels before creating two different plots. Our font size settings will apply to both.

from matplotlib import pyplot, rcParams rcParams["axes.titlesize"] = 22 rcParams["axes.labelsize"] = 14 workgraph = pyplot.figure() pyplot.plot([1, 2, 3, 4], [4, 3, 2, 3]) pyplot.title("Productivity") pyplot.xlabel("Hours") pyplot.ylabel("Work Units") workgraph.savefig("workperhour.png") sleepgraph = pyplot.figure() pyplot.plot([1, 2, 3, 4, 5, 6, 7], [8, 7, 7, 6, 8, 6, 7]) pyplot.title("Sleep") pyplot.xlabel("Day") pyplot.ylabel("Hours") sleepgraph.savefig("sleephoursperday.png")

Global settings can be defined inline, as we’ve done above, or through the creation of a mplstyle file, as detailed in the Matplotlib documentation. Font size and other text properties passed directly to the relevant function, as in the first code example, will override global settings.

Get Started With Sentry

Get actionable, code-level insights to resolve Python performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a Python project and note your DSN

  3. Grab the Sentry Python SDK

pip install --upgrade sentry-sdk
  1. Configure your DSN
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.