CodeGym /Courses /Python SELF EN /Setting Labels, Legends, and Axes for Better Chart Unders...

Setting Labels, Legends, and Axes for Better Chart Understanding

Python SELF EN
Level 42 , Lesson 0
Available

1. The Importance of Annotating Charts

Why Adding Labels, Legends, and Annotations to Charts Matters

Today we're diving into a part of data visualization that's just as important as the charts themselves—annotation. It's like the receiver for a TV: everything seems in place, but it's hard to watch since the picture isn't always clear. Labels, legends, and annotations are sometimes just what we need to make the data come alive and speak to us in a human-readable way.

You might ask: are labels and legends really that important? The answer is simple: they are the salt and pepper of a chart. Without them, your chart could be hard to read, and the meaning of your data might get lost. Adding annotations gives your chart extra meaning and makes it more intuitive to understand.

Labels and legends help your viewers quickly grasp what they’re seeing. Imagine staring at a treasure map without any markers and being asked to find the treasure. Sounds crazy, right? The same goes for charts without labels. We should be helping our viewers, not making their lives harder.

Now let’s dive deeper into how to bring the magic of annotations to your charts using Python and Matplotlib. Ready? Let’s roll!

2. Axis Labels

Managing Axis Labels with xlabel, ylabel, and title

The first step—giving names to the axes. The process is as simple as 2+2. Use the xlabel() and ylabel() functions to add labels to the X and Y axes respectively. And for the chart title, use title(). These functions give your chart structure and clarity. Here’s a basic example:

Python

import matplotlib.pyplot as plt

# Data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

# Creating the chart
plt.plot(x, y, color='blue')

# Adding labels
plt.xlabel('Time (hours)', color='red')
plt.ylabel('Distance (km)', color='green')
plt.title('Distance Growth Over Time', color='gold')

# Displaying the chart
plt.show()

You can also style your text and chart in your favorite colors:

Customizing Font and Size of Labels

To make your chart more attractive, you can tweak the size, color, and style of the labels. In Matplotlib, you do this using parameters like fontsize, fontweight, color, and others.

Python

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 15, 20, 25, 30]

plt.plot(x, y)

# Setting the title and labels with parameters
plt.title("Chart Example", fontsize=16, fontweight="bold", color="navy")
plt.xlabel("X Values", fontsize=12, color="darkred")
plt.ylabel("Y Values", fontsize=12, color="darkred")

plt.show()

In this example:

  • fontsize sets the font size.
  • fontweight="bold" makes the text bold.
  • color changes the text color.

3. Displaying the "Legend"

Adding and Customizing Legends with legend()

The legend isn’t just a myth or story, but a critical tool in your chart toolkit. It explains what the lines or bars in the chart represent and helps distinguish multiple data series. Matplotlib makes this super simple. Here’s how you can add a legend to your chart:

Python

# Data
x = [0, 1, 2, 3, 4, 5]
y1 = [0, 1, 4, 9, 16, 25]
y2 = [0, 1, 2, 3, 4, 5]

# Creating the chart
plt.plot(x, y1, label='Square of Number')
plt.plot(x, y2, label='Number Itself')

# Adding labels
plt.xlabel('Time (hours)')
plt.ylabel('Distance (km)')
plt.title('Function Comparison')

# Adding the legend
plt.legend(loc='upper left')

# Displaying the chart
plt.show()
1
Task
Python SELF EN, level 42, lesson 0
Locked
Basic Chart Annotation
Basic Chart Annotation
2
Task
Python SELF EN, level 42, lesson 0
Locked
Adding Legend and Customizing Axes
Adding Legend and Customizing Axes
3
Task
Python SELF EN, level 42, lesson 0
Locked
Advanced Graph Setup
Advanced Graph Setup
4
Task
Python SELF EN, level 42, lesson 0
Locked
Practical application of annotations and styling of plots
Practical application of annotations and styling of plots
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION