Legend outside the plot
📖 Scenario: You are creating a simple line chart to compare sales data for two products over four quarters. You want to make sure the legend explaining the lines is placed outside the main plot area so the chart looks clean and easy to read.
🎯 Goal: Build a line plot with two lines representing sales of two products. Place the legend outside the plot area on the right side.
📋 What You'll Learn
Create two lists named
quarters and sales_product_a with exact values.Create a list named
sales_product_b with exact values.Plot both sales lines with labels
'Product A' and 'Product B'.Add a legend outside the plot on the right side using
plt.legend with bbox_to_anchor and loc parameters.Display the plot with
plt.show().💡 Why This Matters
🌍 Real World
Placing legends outside the plot is common in reports and dashboards to keep the chart area clean and readable.
💼 Career
Data analysts and scientists often need to create clear visualizations for presentations and reports, including customizing legend placement.
Progress0 / 4 steps