Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the label text inside quotes.
Passing a variable name without defining it.
✗ Incorrect
The xlabel function requires a string argument to label the x-axis. So, the label must be in quotes.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple.
Passing two separate arguments instead of a tuple.
✗ Incorrect
The ylim function expects a tuple with two values for the limits.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling PercentFormatter with parentheses before passing it.
Using the wrong formatter class.
✗ Incorrect
PercentFormatter is a class and should be instantiated with xmax=1 to correctly format values between 0 and 1 as percentages.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The dictionary comprehension maps each word to its length (len(word)) only if the length is greater than 3 (len(word) > 3).
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of w.upper().
Checking length greater than 6 instead of less than 6.
✗ Incorrect
The comprehension maps uppercase words (w.upper()) to their lengths (len(w)) only if the length is less than 6 (len(w) < 6).