0
0
Matplotlibdata~30 mins

Multi-line text in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Adding Multi-line Text to a Matplotlib Plot
📖 Scenario: You are creating a simple plot to show sales data for two products. You want to add a multi-line text note on the plot to explain the data.
🎯 Goal: Build a plot with two lines and add a multi-line text annotation inside the plot area.
📋 What You'll Learn
Create a plot with two lines representing sales data.
Add a multi-line text note inside the plot using plt.text.
Use newline characters \n to create multiple lines in the text.
💡 Why This Matters
🌍 Real World
Adding multi-line text notes on plots helps explain data clearly in reports and presentations.
💼 Career
Data scientists and analysts often annotate plots with multi-line text to highlight insights or provide context.
Progress0 / 4 steps
1
Create sales data lists
Create two lists called sales_product_a and sales_product_b with these exact values: [10, 15, 20, 25] and [12, 18, 22, 28] respectively.
Matplotlib
Need a hint?

Use square brackets to create lists with the exact numbers given.

2
Create x-axis values
Create a list called months with these exact values: [1, 2, 3, 4] representing four months.
Matplotlib
Need a hint?

Use a list with the numbers 1 to 4 for months.

3
Plot the sales data lines
Import matplotlib.pyplot as plt. Use plt.plot to plot months vs sales_product_a and months vs sales_product_b. Add labels 'Product A' and 'Product B' respectively.
Matplotlib
Need a hint?

Use plt.plot(x, y, label='name') to plot lines with labels.

4
Add multi-line text and show plot
Use plt.text to add multi-line text at position (2, 15) with the exact text: 'Sales Data\nProduct A and B'. Then add plt.legend() and plt.show() to display the plot.
Matplotlib
Need a hint?

Use plt.text(x, y, 'line1\nline2') to add multi-line text.