Prerak
Prerak

Reputation: 109

Change fontsize of indivual labels in the matplotlib legend

I would like to change the font size of individual elements in the legend. I know how to change the font properties of the whole legend but I can not figure out a way to change them individually.

For example: In the example below I am plotting two lines and I want the label for line1 to have a bigger font size than line 2

import numpy as np
from matplotlib import pyplot as plt

def line (m , c):
    return m*x + c

x = np.arange(0, 10, .25)
plt.plot(x, line(1, 0), label = "Line 1")
plt.plot(x, line(1,1), label = "Line 2")
plt.legend(prop={'family': 'Georgia', 'size': 15})
plt.show()

Using prop={'family': 'Georgia', 'size': 15} I can modify the font size of both the labels simultaneously but is there a way to control the font properties of individual labels in the legend?

Thanks any and all help is appreciated. enter image description here

Upvotes: 2

Views: 558

Answers (1)

r-beginners
r-beginners

Reputation: 35275

Here are some great answers: one is to set the font properties, and the other is to use Latexh notation for the label settings. I will answer with the method of customizing with font properties.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties

def line (m , c):
    return m*x + c

x = np.arange(0, 10, .25)
plt.plot(x, line(1, 0), label = "Line 1")
plt.plot(x, line(1,1), label = "Line 2")
leg = plt.legend(prop={'family': 'Georgia', 'size': 15})
label1, label2 = leg.get_texts()
label1._fontproperties = label2._fontproperties.copy()
label1.set_size('medium')

plt.show()

enter image description here

Upvotes: 1

Related Questions