0
0
Matplotlibdata~10 mins

Bar width and positioning 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 width of the bars to 0.5.

Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3]
heights = [4, 5, 6]
plt.bar(x, heights, width=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A0
B1.5
C2
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using width greater than 1 makes bars overlap.
Setting width to 0 makes bars invisible.
2fill in blank
medium

Complete the code to position bars at x-coordinates shifted by 0.3 to the right.

Matplotlib
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()
Drag options to blanks, or click blank then click option'
A0.3
B0.1
C-0.3
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using negative values shifts bars left instead of right.
Using 1 shifts bars too far.
3fill in blank
hard

Fix the error in the code to correctly position two sets of bars side by side.

Matplotlib
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()
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction shifts bars left, overlapping the first set.
Using multiplication or division causes errors or wrong positions.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

Matplotlib
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters the wrong words.
Using the word itself instead of its length as the value.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words mapped to their lengths if length is greater than 3.

Matplotlib
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word instead of uppercase as key.
Using < instead of > in the condition.