0
0
Matplotlibdata~10 mins

Why axis formatting matters in Matplotlib - Test Your Understanding

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

Complete the code to set the x-axis label in a matplotlib plot.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.xlabel([1])
plt.show()
Drag options to blanks, or click blank then click option'
A'X Axis'
Bxlabel
CX Axis
Dplt.xlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the label text inside quotes.
Passing a variable name without defining it.
2fill in blank
medium

Complete the code to set the y-axis limits from 0 to 10.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.ylim([1])
plt.show()
Drag options to blanks, or click blank then click option'
A(0, 10)
B[0, 10]
C0, 10
D{0:10}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple.
Passing two separate arguments instead of a tuple.
3fill in blank
hard

Fix the error in the code to correctly format the x-axis ticks as percentages.

Matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

fig, ax = plt.subplots()
ax.plot([0.1, 0.2, 0.3], [1, 2, 3])
ax.xaxis.set_major_formatter([1](xmax=1))
plt.show()
Drag options to blanks, or click blank then click option'
Amtick.FuncFormatter
Bmtick.FormatStrFormatter
Cmtick.PercentFormatter
Dmtick.PercentFormatter()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling PercentFormatter with parentheses before passing it.
Using the wrong formatter class.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.

Matplotlib
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
plt.plot(list(lengths.values()))
plt.show()
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Checking if the word is greater than 3 instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if the length is less than 6.

Matplotlib
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if [3] }
plt.bar(result.keys(), result.values())
plt.show()
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) < 6
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of w.upper().
Checking length greater than 6 instead of less than 6.