📖 Scenario: You are creating a simple color chart to understand how named colors and hex codes work in plotting. This helps when you want to make your charts look nice and clear.
🎯 Goal: Build a small program that stores some named colors and their hex codes, then uses matplotlib to show these colors in a bar chart.
📋 What You'll Learn
Create a dictionary with color names as keys and their hex codes as values
Create a list of color names to use in the chart
Use matplotlib to plot a bar chart with bars colored by their hex codes
Print the dictionary of colors and hex codes
💡 Why This Matters
🌍 Real World
Knowing how to use named colors and hex codes helps you make charts and graphs that are easy to understand and visually appealing.
💼 Career
Data scientists and analysts often customize chart colors to match branding or to highlight important data points clearly.
Progress0 / 4 steps
1
Create a dictionary of colors and hex codes
Create a dictionary called colors with these exact entries: 'red': '#FF0000', 'green': '#00FF00', 'blue': '#0000FF', 'yellow': '#FFFF00', and 'purple': '#800080'.
Matplotlib
Hint
Use curly braces {} to create the dictionary. Each entry is a key-value pair separated by a colon.
2
Create a list of color names to plot
Create a list called color_names containing these exact strings in this order: 'red', 'green', 'blue', 'yellow', 'purple'.
Matplotlib
Hint
Use square brackets [] to create the list with the color names as strings.
3
Plot a bar chart with colors using hex codes
Import matplotlib.pyplot as plt. Then create a bar chart with bars for each color in color_names. Use the hex codes from colors to set the bar colors. Use range(len(color_names)) for x positions and [1]*len(color_names) for bar heights. Set the bar colors with the color parameter using a list comprehension that gets hex codes from colors.
Matplotlib
Hint
Use plt.bar() with x positions as range(len(color_names)) and heights as a list of 1s. Use a list comprehension to get hex codes for colors.
4
Print the colors dictionary and show the plot
Print the colors dictionary. Then call plt.show() to display the bar chart.
Matplotlib
Hint
Use print(colors) to show the dictionary. Use plt.show() to display the chart.
Practice
(1/5)
1. Which of the following is a valid named color in matplotlib?
easy
A. #12345G
B. bluish
C. red
D. 123456
Solution
Step 1: Understand named colors in matplotlib
Matplotlib supports standard color names like 'red', 'blue', 'green', etc.
Step 2: Check each option
red is 'red', a valid named color. #12345G has an invalid hex code with 'G'. bluish 'bluish' is not a recognized name. 123456 is just digits, not a named color.
Final Answer:
red -> Option C
Quick Check:
Named color 'red' is valid [OK]
Hint: Named colors are simple words like 'red' or 'blue' [OK]
Common Mistakes:
Confusing invalid hex codes as named colors
Assuming any color-like word is valid
Using numbers without '#' as colors
2. Which of the following is the correct way to specify a hex color code in matplotlib?
easy
A. "#1a2b3c"
B. "1a2b3c"
C. "#12345"
D. "#xyz123"
Solution
Step 1: Recall hex color code format
Hex codes start with '#' followed by exactly 6 hexadecimal digits (0-9, a-f).
Step 2: Validate each option
"#1a2b3c" has '#' and 6 valid hex digits. "1a2b3c" misses '#'. "#12345" has only 5 digits. "#xyz123" contains invalid letters 'x' and 'z'.
Final Answer:
"#1a2b3c" -> Option A
Quick Check:
Hex code must start with '#' and have 6 hex digits [OK]
Hint: Hex codes start with '#' and have 6 hex digits [OK]
Common Mistakes:
Omitting the '#' symbol
Using wrong number of digits
Including invalid characters
3. What color will the following matplotlib code produce?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], color="#00FF00")
plt.show()
medium
A. Black
B. Blue
C. Red
D. Green
Solution
Step 1: Understand the hex code used
"#00FF00" is a hex code where red=00, green=FF, blue=00, meaning full green.
Step 2: Match hex code to color
Since green is max and others zero, the line color will be bright green.
Final Answer:
Green -> Option D
Quick Check:
"#00FF00" means green color [OK]
Hint: Hex #00FF00 means full green, no red or blue [OK]
Common Mistakes:
Confusing hex digits order
Assuming #00FF00 is blue
Ignoring case sensitivity in hex
4. Identify the error in this matplotlib color specification:
plt.plot([1, 2, 3], color="#123ABZ")
medium
A. Missing '#' in hex code
B. Invalid hex code due to 'Z'
C. Color name not recognized
D. Syntax error in plot function
Solution
Step 1: Check hex code characters
Hex codes only allow digits 0-9 and letters A-F (case insensitive). 'Z' is invalid.
Step 2: Confirm error type
The code has '#' and correct length, but 'Z' makes it invalid hex code.
Final Answer:
Invalid hex code due to 'Z' -> Option B
Quick Check:
Hex digits must be 0-9 or A-F only [OK]
Hint: Hex codes only use digits 0-9 and letters A-F [OK]
Common Mistakes:
Ignoring invalid characters in hex
Confusing missing '#' with invalid chars
Assuming any letter is allowed in hex
5. You want to plot two lines in matplotlib: one red and one with a custom color #FF8800. Which code snippet correctly sets these colors?
hard
A. plt.plot(x, y1, color='red')
plt.plot(x, y2, color='#FF8800')
B. plt.plot(x, y1, color='#red')
plt.plot(x, y2, color='FF8800')
C. plt.plot(x, y1, color='ff8800')
plt.plot(x, y2, color='red')
D. plt.plot(x, y1, color='red')
plt.plot(x, y2, color='FF8800')
Solution
Step 1: Check named color usage
Named colors like 'red' are used as strings without '#'. Adding '#' to named colors makes them invalid.
Step 2: Check hex code usage
Hex codes must start with '#'. Omitting '#' for hex colors like 'FF8800' or using lowercase without '#' is invalid.
Final Answer:
plt.plot(x, y1, color='red')
plt.plot(x, y2, color='#FF8800') -> Option A
Quick Check:
Named colors no '#', hex codes start with '#' [OK]
Hint: Named colors no '#'; hex codes start with '#' [OK]
Common Mistakes:
Adding '#' to named colors
Omitting '#' in hex codes
Using lowercase hex without '#'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.