📖 Scenario: You work in a small bakery and want to show how many cakes you sold each day last week. You want to make a bar chart to show this clearly to your team.
🎯 Goal: Create a bar chart using matplotlib that shows the number of cakes sold each day. Customize the colors of the bars so that each day has a different color.
📋 What You'll Learn
Create a dictionary called sales with days of the week as keys and number of cakes sold as values.
Create a list called colors with a different color for each day.
Use matplotlib.pyplot.bar() to make a bar chart with the sales data and the colors list.
Display the bar chart with plt.show().
💡 Why This Matters
🌍 Real World
Businesses often use bar charts to show sales or performance data clearly to teams and customers.
💼 Career
Data analysts and scientists use color customization in charts to make reports easier to understand and visually appealing.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales with these exact entries: 'Monday': 20, 'Tuesday': 35, 'Wednesday': 30, 'Thursday': 25, 'Friday': 40, 'Saturday': 50, 'Sunday': 45.
Matplotlib
Hint
Use curly braces {} to create the dictionary with the exact days and numbers.
2
Create a list of colors for the bars
Create a list called colors with these exact color names in order: 'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'.
Matplotlib
Hint
Make sure the list has exactly these colors in this order.
3
Create the bar chart with colors
Import matplotlib.pyplot as plt. Use plt.bar() with the days from sales.keys(), the cake counts from sales.values(), and the colors list to set the bar colors.
Matplotlib
Hint
Use plt.bar() with the color parameter set to the colors list.
4
Display the bar chart
Use plt.show() to display the bar chart you created.
Matplotlib
Hint
Call plt.show() to display the chart window.
Practice
(1/5)
1. What does the color parameter do in the plt.bar() function in matplotlib?
easy
A. It changes the color of the bars in the bar chart.
B. It changes the size of the bars.
C. It adds labels to the bars.
D. It changes the style of the axes.
Solution
Step 1: Understand the role of plt.bar()
The plt.bar() function draws bars for a bar chart.
Step 2: Identify the color parameter effect
The color parameter sets the color of the bars, either one color for all or a list for each bar.
Final Answer:
It changes the color of the bars in the bar chart. -> Option A
Quick Check:
color changes bar colors [OK]
Hint: Remember: color = bar colors in plt.bar() [OK]
Common Mistakes:
Confusing color with size or labels
Thinking color changes axes or grid
Using color parameter outside plt.bar()
2. Which of the following is the correct syntax to set all bars to blue in a bar chart using matplotlib?
easy
A. plt.bar(x, height, bar_color='blue')
B. plt.bar(x, height, color='blue')
C. plt.bar(x, height, col='blue')
D. plt.bar(x, height, colors='blue')
Solution
Step 1: Recall the correct parameter name
The parameter to set bar colors is color, not colors, col, or bar_color.
Step 2: Check the syntax for setting a single color
Using color='blue' applies blue color to all bars.
Final Answer:
plt.bar(x, height, color='blue') -> Option B
Quick Check:
Use color parameter exactly [OK]
Hint: Use exact parameter name: color='colorname' [OK]
Common Mistakes:
Using 'colors' instead of 'color'
Using 'col' or 'bar_color' which are invalid
Misspelling 'color' parameter
3. What will be the color of each bar in the following code?
A. The colors list has fewer colors than bars, causing an error.
B. The colors list should be a tuple, not a list.
C. There is no error; the code runs fine.
D. The color parameter should be named colors.
Solution
Step 1: Check length of colors list vs bars
There are 3 bars but only 2 colors provided.
Step 2: Understand matplotlib behavior
Matplotlib allows a shorter list of colors and cycles through them to color bars.
Final Answer:
There is no error; the code runs fine. -> Option C
Quick Check:
Colors list cycles if shorter than bars [OK]
Hint: Matplotlib cycles colors if list shorter than bars [OK]
Common Mistakes:
Using fewer colors than bars
Wrong parameter name 'colors'
Assuming matplotlib throws error on shorter color list
5. You want to create a bar chart with 5 bars where bars at even positions are blue and odd positions are orange. Which code correctly customizes the bar colors?