Chapter 4 – Legends, Labels, and Annotations in Matplotlib
Now that you have learned how to customize colors, styles, and layouts in Matplotlib, it’s time to make your charts more readable and professional. In this chapter, we’ll explore how to add legends, labels, titles, and annotations — all essential for telling a clear visual story.
✅ Why Legends, Labels, and Annotations Matter
Visualizations are not just about drawing lines or bars — they are about communication. When someone looks at your chart, they should instantly understand what the data represents. Legends and labels act like a map; they guide the viewer to interpret your visualization correctly. Annotations, on the other hand, help you highlight key points or insights within your data.
For example, if you’re comparing the growth of two companies, a legend tells which line belongs to which company. Labels describe the axes, and annotations can point out the year when profits peaked. Together, these features make a chart not just pretty but powerful.
✅ Adding Titles and Axis Labels
Let’s begin with the basics — giving your chart a title and labeling your axes. Without labels, even an accurate chart can become confusing.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [5, 7, 6, 8, 7]
plt.plot(x, y, color='blue', marker='o')
plt.title("Monthly Revenue Trend")
plt.xlabel("Month")
plt.ylabel("Revenue ($ in thousands)")
plt.show()
This code gives your chart a title and names for both the x-axis and y-axis. You can customize fonts, sizes, and colors too:
plt.title("Monthly Revenue Trend", fontsize=14, color='darkblue', pad=15)
plt.xlabel("Month", fontsize=12, color='gray')
plt.ylabel("Revenue ($ in thousands)", fontsize=12, color='gray')
The pad parameter adds space between the title and the plot area, ensuring readability.
✅ Adding Legends
A legend explains what each plotted line, bar, or scatter group represents. It’s essential when you have multiple datasets on one chart.
x = [1, 2, 3, 4, 5]
sales_A = [5, 6, 7, 8, 9]
sales_B = [3, 4, 6, 7, 8]
plt.plot(x, sales_A, color='blue', marker='o', label='Product A')
plt.plot(x, sales_B, color='green', marker='s', label='Product B')
plt.title("Monthly Sales Comparison")
plt.xlabel("Month")
plt.ylabel("Sales (in 1000 units)")
plt.legend()
plt.show()
In this example, the label parameter gives each line a name, and plt.legend() displays those labels inside the chart. Matplotlib automatically finds a good position for the legend, but you can control it manually:
plt.legend(loc='upper left')
Common legend locations include:
upper leftupper rightlower leftlower rightcenterbest— automatic placement
You can also customize the legend appearance:
plt.legend(title="Products", fontsize=10, title_fontsize=11, shadow=True, fancybox=True)
fancybox adds rounded corners, and shadow gives a subtle 3D effect — great for presentations or reports.
✅ Legends Outside the Plot
Sometimes, your chart is too crowded and the legend overlaps with data. In such cases, placing the legend outside can improve readability:
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
plt.tight_layout()
plt.show()
The bbox_to_anchor argument positions the legend just outside the plot, and tight_layout() adjusts spacing automatically.
✅ Annotating Important Points
Annotations allow you to highlight specific points or add text explanations directly on the chart. This is extremely useful when you want to emphasize trends or anomalies
