Complete the code to set the width of the bars to 0.5.
import matplotlib.pyplot as plt x = [1, 2, 3] heights = [4, 5, 6] plt.bar(x, heights, width=[1]) plt.show()
The width parameter controls the thickness of the bars. Setting it to 0.5 makes the bars half as wide as the default.
Complete the code to position bars at x-coordinates shifted by 0.3 to the right.
import matplotlib.pyplot as plt x = [1, 2, 3] heights = [4, 5, 6] plt.bar([i + [1] for i in x], heights, width=0.5) plt.show()
Adding 0.3 shifts the bars to the right by 0.3 units on the x-axis.
Fix the error in the code to correctly position two sets of bars side by side.
import matplotlib.pyplot as plt x = [1, 2, 3] heights1 = [4, 5, 6] heights2 = [3, 4, 5] width = 0.4 plt.bar(x, heights1, width=width, label='A') plt.bar([i [1] width for i in x], heights2, width=width, label='B') plt.legend() plt.show()
Adding width shifts the second set of bars to the right, placing them side by side.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary comprehension maps each word to its length only if the word length is greater than 3.
Fill all three blanks to create a dictionary of uppercase words mapped to their lengths if length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] result = { [1]: [2] for word in words if len(word) [3] 3 }
This dictionary comprehension creates keys as uppercase words and values as their lengths, only for words longer than 3 characters.