Matplotlib – Label fontdict

Matplotlib – Label fontdict

In Matplotlib, the fontdict parameter allows you to customize the font properties of labels, providing control over aspects like font size, style, weight, and more.

Example for fontdict

Let us write an example program with a plot, where we set the font family, font color, font weight, and font size X-label and Y-label using fontdict parameter of xlabel() and ylabel() functions respectively.

Python Program

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 55, 40]

# Plot the data
plt.plot(x, y, marker='o')

# Add labels to the X and Y axes with custom font properties
font_properties = {'family': 'serif', 'color': 'green', 'weight': 'bold', 'size': 15}
plt.xlabel('X-axis Label', fontdict=font_properties)
plt.ylabel('Y-axis Label', fontdict=font_properties)

# Show the plot
plt.show()

In this example, the fontdict parameter is used to customize the font properties for the X and Y axis labels. The font_properties dictionary contains specifications such as font family ('serif'), color ('blue'), font weight ('bold'), and size (12). Adjust these properties according to your preferences.

Output

Matplotlib - Label fontdict
Code copied to clipboard successfully 👍