WikiGalaxy

Personalize

Adding Titles, Labels, and Legends

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. When creating plots, it is essential to add titles, labels, and legends to make the data more understandable and to provide context. Titles give an overview of what the plot is about, labels describe the axes, and legends explain what each plot line represents.

Basic Plot with Title and Labels

In this example, we will create a simple line plot with a title and axis labels. This helps in understanding the data being plotted.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.plot(x, y)
plt.title('Prime Numbers')
plt.xlabel('Index')
plt.ylabel('Value')
plt.show()
        

The code above creates a line plot of prime numbers. The plt.title() function adds a title to the plot, while plt.xlabel() and plt.ylabel() add labels to the x-axis and y-axis respectively.

Adding Legends to Plots

Legends are used to identify different data series in a plot. In this example, we will add legends to a plot with multiple lines.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 8, 27, 64, 125]

plt.plot(x, y1, label='Squares')
plt.plot(x, y2, label='Cubes')
plt.title('Powers of Numbers')
plt.xlabel('Number')
plt.ylabel('Value')
plt.legend()
plt.show()
        

In this code, the label argument in the plt.plot() function is used to define the legend. The plt.legend() function displays the legend on the plot, helping to distinguish between the squares and cubes series.

Customizing Legend Location

The legend's position can be customized to avoid overlapping with the plot data. Here, we show how to place the legend at a specific location.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [3, 6, 9, 12, 15]

plt.plot(x, y1, label='Doubles')
plt.plot(x, y2, label='Triples')
plt.title('Multiples of Numbers')
plt.xlabel('Number')
plt.ylabel('Value')
plt.legend(loc='upper left')
plt.show()
        

The loc parameter in plt.legend() specifies the legend's location. In this example, setting loc='upper left' places the legend in the upper left corner of the plot.

Using Subplots with Titles and Labels

Subplots allow multiple plots to be displayed in a single figure. Each subplot can have its own title and labels.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [1, 4, 9, 16, 25]

plt.subplot(1, 2, 1)
plt.plot(x, y1)
plt.title('Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

plt.subplot(1, 2, 2)
plt.plot(x, y2)
plt.title('Quadratic Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

plt.tight_layout()
plt.show()
        

The plt.subplot() function is used to create subplots. The plt.tight_layout() function adjusts the subplots to fit into the figure area, ensuring that titles and labels do not overlap.

Advanced Title Customization

Titles can be customized with different fonts, sizes, and colors to enhance the plot's appearance. Here, we demonstrate how to customize a plot title.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]

plt.plot(x, y)
plt.title('Customized Title', fontsize=14, fontweight='bold', color='purple')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
        

The fontsize, fontweight, and color parameters in plt.title() allow for customization of the title's appearance, making it stand out and match the overall theme of the plot.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025