0
0
Matplotlibdata~10 mins

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

Choose your learning style9 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.