The plt.pie() function accepts sizes as list and labels as list without error.
Step 2: Verify optional parameters
Title and colors are optional; missing them does not cause errors.
Final Answer:
No error, code will run and show pie chart -> Option B
Quick Check:
plt.pie() works with lists and labels [OK]
Hint: Pie chart needs sizes and labels only, colors optional [OK]
Common Mistakes:
Thinking title is mandatory
Believing sizes must be numpy arrays
Assuming colors argument is required
5. You want to plot a bar chart on Raspberry Pi showing sales for products A, B, and C with values 5, 7, and 3. Which code correctly creates this chart with labels and title?
hard
A. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.scatter(products, sales)
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales Bar Chart')
plt.show()
B. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.plot(products, sales)
plt.xlabel('Sales')
plt.ylabel('Products')
plt.title('Sales Bar Chart')
plt.show()
C. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.bar(products, sales)
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales Bar Chart')
plt.show()
D. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.pie(sales, labels=products)
plt.title('Sales Bar Chart')
plt.show()
Solution
Step 1: Identify correct chart type for sales data
Bar charts show quantities per category, so plt.bar() is correct.
Step 2: Check axis labels and title
Labels for products (x-axis) and sales (y-axis) match the data meaning.
Step 3: Verify other options
import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.plot(products, sales)
plt.xlabel('Sales')
plt.ylabel('Products')
plt.title('Sales Bar Chart')
plt.show() uses line plot with swapped labels, C uses scatter plot, D uses pie chart which is different visualization.
Final Answer:
Code using plt.bar() with correct labels and title -> Option C
Quick Check:
Bar chart = plt.bar() with correct labels [OK]
Hint: Use plt.bar() for bar charts, label axes clearly [OK]
Common Mistakes:
Using plt.plot() or plt.scatter() for bar charts
Swapping x and y axis labels
Confusing pie chart with bar chart
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.