0
0
Data Analysis Pythondata~5 mins

Labels, titles, and legends in Data Analysis Python

Choose your learning style9 modes available
Introduction

Labels, titles, and legends help explain what a chart or graph shows. They make data easy to understand.

When you want to show what each axis means in a graph.
When you want to give a clear title to your chart.
When you have multiple data lines or bars and want to explain each one.
When sharing charts with others who need to understand the data quickly.
Syntax
Data Analysis Python
import matplotlib.pyplot as plt

plt.plot(x, y, label='Line 1')
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Chart Title')
plt.legend()
plt.show()
Use xlabel() and ylabel() to add labels to the x and y axes.
Use title() to add a main title to the chart.
Use legend() to show the labels for different data series.
Examples
A basic line chart with labels on x and y axes and a title.
Data Analysis Python
plt.plot([1, 2, 3], [4, 5, 6])
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Simple Line Chart')
plt.show()
Two lines with labels and a legend to explain each line.
Data Analysis Python
plt.plot([1, 2, 3], [4, 5, 6], label='Data 1')
plt.plot([1, 2, 3], [6, 5, 4], label='Data 2')
plt.legend()
plt.title('Two Lines with Legend')
plt.show()
Sample Program

This program plots two lines showing sales for two years. It adds labels for the x and y axes, a title, and a legend to explain the lines.

Data Analysis Python
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [15, 18, 22, 28]

# Plot first line
plt.plot(x, y1, label='Sales 2023')
# Plot second line
plt.plot(x, y2, label='Sales 2024')

# Add labels and title
plt.xlabel('Quarter')
plt.ylabel('Revenue (in thousands)')
plt.title('Quarterly Sales Comparison')

# Show legend
plt.legend()

# Display the plot
plt.show()
OutputSuccess
Important Notes
Always add labels and titles to make your charts clear.
Legends are important when you have multiple data series.
You can customize label fonts and colors for better look.
Summary

Labels explain what each axis means.

Titles give the chart a clear heading.

Legends describe different data lines or groups.