0
0
Matplotlibdata~10 mins

Named colors and hex codes in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the line color to a named color 'red' in matplotlib.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6], color='[1]')
plt.show()
Drag options to blanks, or click blank then click option'
Acyan
Bred
C#00FF00
Dblue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a hex code instead of a named color when the instruction asks for a named color.
Forgetting to put the color name inside quotes.
2fill in blank
medium

Complete the code to set the marker color using a hex code for green.

Matplotlib
import matplotlib.pyplot as plt
plt.scatter([1, 2, 3], [4, 5, 6], marker='o', color='[1]')
plt.show()
Drag options to blanks, or click blank then click option'
Ared
Byellow
Cblue
D#00FF00
Attempts:
3 left
💡 Hint
Common Mistakes
Using a named color instead of a hex code when the instruction asks for a hex code.
Missing the '#' symbol in the hex code.
3fill in blank
hard

Fix the error in the code by choosing the correct color format for a blue line.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1], color=[1])
plt.show()
Drag options to blanks, or click blank then click option'
Ablue
B'blue'
C'#0000FF'
D#0000FF
Attempts:
3 left
💡 Hint
Common Mistakes
Passing color without quotes causing a syntax error.
Using named color without quotes.
4fill in blank
hard

Fill both blanks to create a dictionary mapping color names to their hex codes.

Matplotlib
colors = {'red': '[1]', 'green': '[2]'}
Drag options to blanks, or click blank then click option'
A#FF0000
B#00FF00
Cred
Dgreen
Attempts:
3 left
💡 Hint
Common Mistakes
Using color names instead of hex codes in the dictionary values.
Forgetting quotes around hex codes.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps color names to their uppercase hex codes if the hex code starts with '#'.

Matplotlib
colors = {'red': '#ff0000', 'blue': '#0000ff', 'green': '00ff00'}
result = {k: v[1]() for k, v in colors.items() if v[2]('#')}
print(result)  # Output: [3]
Drag options to blanks, or click blank then click option'
A.upper
B[0]
C{'red': '#FF0000', 'blue': '#0000FF'}
Dstartswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using indexing [0] instead of startswith to check the first character.
Not calling the upper method with parentheses.
Including colors without '#' in the output.