Bird
Raised Fist0
Matplotlibdata~10 mins

Named colors and hex codes in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Named colors and hex codes
Start
↓
Choose color type
↓
Named
↓
Use color in plot
↓
Display plot
↓
End
This flow shows choosing a color by name or hex code, using it in a plot, and displaying the result.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2,3], color='red')
plt.plot([3,2,1], color='#00FF00')
plt.show()
This code plots two lines, one in named color 'red' and one in hex color '#00FF00' (green), then shows the plot.
Execution Table
StepActionColor InputColor UsedPlot Element
1Plot first line'red'Red (RGB: 1,0,0)Line 1 in red
2Plot second line'#00FF00'Green (RGB: 0,1,0)Line 2 in green
3Show plotN/AN/APlot displayed with two colored lines
💡 Plot displayed with lines colored by named color and hex code.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
colorNone'red''#00FF00'N/A
line_colors[]['red']['red', '#00FF00']['red', '#00FF00']
Key Moments - 2 Insights
Why can I use both 'red' and '#00FF00' as colors?
Matplotlib accepts colors by name (like 'red') or by hex code (like '#00FF00'). Both represent colors but in different formats, as shown in execution_table steps 1 and 2.
What does the hex code '#00FF00' mean?
It is a hex color code representing green. The first two digits are red, next two green, last two blue. '#00FF00' means no red, full green, no blue, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what color is used for the first line plotted?
ABlue
BGreen
CRed
DBlack
💡 Hint
Check Step 1 in the execution_table under 'Color Used'.
At which step is the hex code '#00FF00' used?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the 'Color Input' column in execution_table.
If you change the color 'red' to '#FF0000', what happens in the variable_tracker?
AThe color value changes after Step 2
BThe color value changes from 'red' to '#FF0000' after Step 1
CThe color value stays 'red' after Step 1
DNo change in color values
💡 Hint
Check the 'color' variable changes in variable_tracker after Step 1.
Concept Snapshot
Named colors and hex codes in matplotlib:
- Use named colors like 'red', 'blue', 'green'.
- Use hex codes like '#FF0000' for red.
- Both specify colors for plot elements.
- Hex codes are #RRGGBB format.
- Matplotlib accepts both interchangeably.
Full Transcript
This lesson shows how matplotlib accepts colors by name or hex code. The code plots two lines: one with the named color 'red' and one with the hex code '#00FF00' which is green. The execution table traces each step: plotting the first line with 'red', the second with '#00FF00', and then showing the plot. The variable tracker shows how the color variables change after each step. Key moments clarify why both color formats work and what hex codes mean. The quiz tests understanding of which colors are used and how changing color inputs affects variables. The snapshot summarizes how to use named colors and hex codes in matplotlib plots.

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

  1. Step 1: Understand named colors in matplotlib

    Matplotlib supports standard color names like 'red', 'blue', 'green', etc.
  2. 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.
  3. Final Answer:

    red -> Option C
  4. 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

  1. Step 1: Recall hex color code format

    Hex codes start with '#' followed by exactly 6 hexadecimal digits (0-9, a-f).
  2. 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'.
  3. Final Answer:

    "#1a2b3c" -> Option A
  4. 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

  1. Step 1: Understand the hex code used

    "#00FF00" is a hex code where red=00, green=FF, blue=00, meaning full green.
  2. Step 2: Match hex code to color

    Since green is max and others zero, the line color will be bright green.
  3. Final Answer:

    Green -> Option D
  4. 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

  1. Step 1: Check hex code characters

    Hex codes only allow digits 0-9 and letters A-F (case insensitive). 'Z' is invalid.
  2. Step 2: Confirm error type

    The code has '#' and correct length, but 'Z' makes it invalid hex code.
  3. Final Answer:

    Invalid hex code due to 'Z' -> Option B
  4. 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

  1. Step 1: Check named color usage

    Named colors like 'red' are used as strings without '#'. Adding '#' to named colors makes them invalid.
  2. Step 2: Check hex code usage

    Hex codes must start with '#'. Omitting '#' for hex colors like 'FF8800' or using lowercase without '#' is invalid.
  3. Final Answer:

    plt.plot(x, y1, color='red') plt.plot(x, y2, color='#FF8800') -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard