How to set font size of Matplotlib axis Legend?

How to set font size of Matplotlib axis Legend?

You can set the font size of the legend in a Matplotlib plot by using the fontsize parameter of the legend function. Here's how you can do it:

import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a plot plt.plot(x, y, label='Data') # Add a legend and set the font size plt.legend(fontsize=12) # Adjust the fontsize to your desired value # Show the plot plt.show() 

In the example above, we first create a plot and then add a legend with plt.legend(). Inside the legend() function, you can set the fontsize parameter to the desired font size. Adjust the value of fontsize to your preferred font size. This will change the font size of the legend text.

You can also set the font size for the legend title (if present) using the title_fontsize parameter:

plt.legend(fontsize=12, title='Legend Title', title_fontsize=14) 

In this case, we set the font size for both the legend text and the legend title.

Examples

  1. "Matplotlib axis Legend font size change"

    • Description: Learn how to adjust the font size of the legend in a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Customize legend font size plt.legend(fontsize='large') plt.show() 
  2. "Matplotlib change font size of legend text"

    • Description: Modify the font size of the text within the legend of a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Adjust legend font size plt.legend(prop={'size': 12}) plt.show() 
  3. "How to set font size of legend in Matplotlib axis?"

    • Description: Set the font size of the legend specifically for the axis in a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Customize legend font size for axis plt.legend(fontsize='x-large', title_fontsize='large') plt.show() 
  4. "How to increase font size of Matplotlib axis Legend?"

    • Description: Increase the font size of the legend displayed in the axis of a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Adjust legend font size plt.legend(fontsize=15) plt.show() 
  5. "Matplotlib adjust font size of legend on axis"

    • Description: Learn how to adjust the font size of the legend specifically for the axis in a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Customize legend font size for axis plt.legend(fontsize='medium') plt.show() 
  6. "Change font size of legend text in Matplotlib axis"

    • Description: Change the font size of the text within the legend specifically for the axis in a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Adjust legend font size for axis plt.legend(fontsize='small') plt.show() 
  7. "How to customize font size of Matplotlib axis Legend?"

    • Description: Customize the font size of the legend specifically for the axis in a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Customize legend font size for axis plt.legend(fontsize='x-small') plt.show() 
  8. "Matplotlib axis Legend font size adjustment"

    • Description: Adjust the font size of the legend displayed in the axis of a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Adjust legend font size plt.legend(fontsize='large') plt.show() 
  9. "Increase font size of Matplotlib axis Legend text"

    • Description: Increase the font size of the text within the legend displayed in the axis of a Matplotlib plot.
    • Code:
      import matplotlib.pyplot as plt # Generate sample data x = [1, 2, 3] y = [4, 5, 6] # Plot data plt.plot(x, y, label='Sample Data') # Customize legend font size for axis plt.legend(fontsize='x-large') plt.show() 
  10. "Matplotlib adjust font size of legend on axis"


More Tags

squirrel-sql pentaho launch4j android-proguard geodjango collider symbols asp.net-core-3.0 laravel-queue asp.net-core-webapi

More Python Questions

More Cat Calculators

More Statistics Calculators

More Mixtures and solutions Calculators

More Everyday Utility Calculators