Editor

Prepare Data for a Multi-Series Chart

Intermediate · Write the code · from Matplotlib: Create Line, Bar, Scatter, and Pie Charts

From the lesson: Matplotlib: Create Line, Bar, Scatter, and Pie Charts

Imagine you have a spreadsheet with 10,000 rows of sales data. You could stare at those numbers for hours, or you could turn them into a chart and spot the trend in three seconds. That's the power of data visualization.

Read the lesson

You have quarterly revenue data for three products. Create a grouped bar chart by computing the x-positions for each group. Given quarters (4 items) and bar width 0.25, calculate x positions for three product groups so bars sit side by side. Print the x positions for each product as lists (use .tolist() to convert numpy arrays). Product A should be at positions [0, 1, 2, 3], Product B at [0.25, 1.25, 2.25, 3.25], and Product C at [0.5, 1.5, 2.5, 3.5].

import numpy as np
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
revenue_a = [50, 60, 70, 80]
revenue_b = [40, 55, 65, 75]
revenue_c = [30, 45, 55, 70]
width = 0.25
x = np.arange(len(quarters)) # [0, 1, 2, 3]
# Calculate positions for each product group
pos_a = x # First group at base positions
# Calculate pos_b and pos_c
print(pos_a.tolist())
# Print pos_b and pos_c as lists

More Matplotlib exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz