6.6 C
London
Thursday, December 19, 2024
HomePythonMatplotlib in PythonHow to Change Legend Font Size in Matplotlib

How to Change Legend Font Size in Matplotlib

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can easily add a plot to a Matplotlib plot by using the following code:

import matplotlib.pyplot as plt

#add legend to plot
plt.legend()

And you can easily change the font size of the text in the legend by using one of the following methods:

Method 1: Specify a Size in Numbers

You can specify font size by using a number:

plt.legend(fontsize=18)

Method 2: Specify a Size in Strings

You can also specify font size by using strings:

plt.legend(fontsize="small")

Options include:

  • xx-small
  • x-small
  • small
  • medium
  • large
  • x-large
  • xx-large

The following examples show how to use each of these methods in practice:

Example 1: Specify Font Size Using a Number

The following example shows how to specify a legend font size using a number:

import matplotlib.pyplot as plt

#create data
plt.plot([2, 4, 6, 11], label="First Line")
plt.plot([1, 4, 5, 9], label="Second Line")

#add legend
plt.legend(fontsize=18)

#show plot
plt.show()

Change legend font size in Matplotlib

Example 2: Specify Font Size Using a String

The following example shows how to specify a legend font size using a string:

import matplotlib.pyplot as plt

#create data
plt.plot([2, 4, 6, 11], label="First Line")
plt.plot([1, 4, 5, 9], label="Second Line")

#add legend
plt.legend(fontsize="small")

#show plot
plt.show()

Change font size of legend in Matplotlib Plot in Python

Additional Resources

How to Change Font Sizes on a Matplotlib Plot
How to Add Text to Matplotlib Plots

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories