Custom legend entries
📖 Scenario: You are creating a simple line chart to compare sales data for two products over four quarters. You want to add a legend to explain the lines, but you want to customize the legend entries to use your own labels and colors.
🎯 Goal: Build a line chart with two lines and add a custom legend with your own labels and colors that do not depend on the plotted lines' default labels.
📋 What You'll Learn
Create two lists of sales data for Product A and Product B for four quarters.
Create a list of quarter labels for the x-axis.
Plot the sales data for both products using matplotlib's
plot function.Create a custom legend with exactly two entries using
matplotlib.patches.Patch for the legend handles.Use the custom legend handles and labels in
plt.legend().Display the plot with
plt.show().💡 Why This Matters
🌍 Real World
Custom legends help make charts clearer and more professional, especially when default labels are not descriptive or when you want to highlight specific information.
💼 Career
Data scientists and analysts often customize legends in reports and presentations to improve communication and make visualizations easier to understand.
Progress0 / 4 steps