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

**URL:** https://sentry.io/answers/set-individual-font-sizes-for-labels-and-titles-in-pyplot-graphs/

---

## The Problem

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

```python
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](https://matplotlib.org/stable/users/explain/text/text_props.html). In this case, we will use the `fontsize` property:

```python
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`](https://matplotlib.org/stable/users/explain/customizing.html#defining-your-own-style). 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.

```python
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](https://matplotlib.org/stable/users/explain/customizing.html#defining-your-own-style). Font size and other text properties passed directly to the relevant function, as in the first code example, will override global settings.

---

*Source: [sentry.io/answers/set-individual-font-sizes-for-labels-and-titles-in-pyplot-graphs/](https://sentry.io/answers/set-individual-font-sizes-for-labels-and-titles-in-pyplot-graphs/)*
