David Y.
—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?
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.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.