Complete the code to set the color of the bars to blue.
import matplotlib.pyplot as plt values = [3, 7, 5] plt.bar([1, 2, 3], values, color=[1]) plt.show()
The color parameter sets the color of the bars. Here, 'blue' colors all bars blue.
Complete the code to set different colors for each bar using a list.
import matplotlib.pyplot as plt values = [4, 6, 8] colors = ['red', 'green', 'blue'] plt.bar([1, 2, 3], values, color=[1]) plt.show()
Passing a list of colors to color colors each bar differently.
Fix the error in the code to correctly set the bar colors using a dictionary.
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()
color_map directly causes an error.The colors list contains the colors for each bar, so it should be passed to color.
Fill both blanks to create a bar chart with bars colored red if value > 5, else green.
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()
The condition checks if each value is greater than 5 to assign 'red', otherwise 'green'.
Fill all three blanks to create a dictionary of bar heights squared for bars with height > 3, using uppercase keys.
values = {'a': 2, 'b': 5, 'c': 7}
squares = [1]: [2]**2 for [3] in values if values[[3]] > 3This dictionary comprehension creates keys as uppercase letters and values as squares of the original values greater than 3.