0
0
Matplotlibdata~10 mins

Bar chart color customization 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 color of the bars to blue.

Matplotlib
import matplotlib.pyplot as plt

values = [3, 7, 5]
plt.bar([1, 2, 3], values, color=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'green'
B'red'
C'blue'
D'yellow'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the color name.
Using a color name not recognized by matplotlib.
2fill in blank
medium

Complete the code to set different colors for each bar using a list.

Matplotlib
import matplotlib.pyplot as plt

values = [4, 6, 8]
colors = ['red', 'green', 'blue']
plt.bar([1, 2, 3], values, color=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
Bcolors
Cvalues
D'red'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single color string instead of a list.
Passing the values list instead of colors.
3fill in blank
hard

Fix the error in the code to correctly set the bar colors using a dictionary.

Matplotlib
import matplotlib.pyplot as plt

values = [5, 3, 9]
color_map = {1: 'cyan', 2: 'magenta', 3: 'yellow'}
colors = [color_map[i] for i in [1, 2, 3]]
plt.bar([1, 2, 3], values, color=[1])
plt.show()
Drag options to blanks, or click blank then click option'
Acolors
B[color_map]
Ccolor_map
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the dictionary color_map directly causes an error.
Passing the values list instead of colors.
4fill in blank
hard

Fill both blanks to create a bar chart with bars colored red if value > 5, else green.

Matplotlib
import matplotlib.pyplot as plt

values = [2, 7, 4, 9]
colors = ['red' if value [1] 5 else [2] for value in values]
plt.bar(range(len(values)), values, color=colors)
plt.show()
Drag options to blanks, or click blank then click option'
A>
B<
C'green'
D'blue'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Forgetting quotes around the color names.
5fill in blank
hard

Fill all three blanks to create a dictionary of bar heights squared for bars with height > 3, using uppercase keys.

Matplotlib
values = {'a': 2, 'b': 5, 'c': 7}
squares = [1]: [2]**2 for [3] in values if values[[3]] > 3
Drag options to blanks, or click blank then click option'
Ak.upper()
Bvalues[k]
Ck
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable names.
Not converting keys to uppercase.