Sentry Answers>Python>

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

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

David Y.

The ProblemJump To Solution

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

Click to Copy
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:

Click to Copy
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.

Click to Copy
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.

  • Sentry BlogPython Performance Testing: A Comprehensive Guide
  • Sentry BlogLogging in Python: A Developer’s Guide
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty Treats for Web Developers brought to you by Sentry. Web development tips and tricks hosted by Wes Bos and Scott Tolinski

    Listen to Syntax

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.

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