Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a plot with two y-axes using matplotlib.
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y1 = [10, 20, 30, 40, 50] y2 = [100, 200, 300, 400, 500] fig, ax1 = plt.subplots() ax2 = ax1.[1]() ax1.plot(x, y1, 'g-') ax2.plot(x, y2, 'b-') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using twiny() creates a twin x-axis, not y-axis.
Using subplot() or subplots() creates new plots, not twin axes.
✗ Incorrect
The method twinx() creates a second y-axis sharing the same x-axis.
2fill in blank
mediumComplete the code to set the y-axis label for the second y-axis.
Matplotlib
ax2.[1]('Second Y Axis')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using set_xlabel() sets the x-axis label instead.
Using label or set_title() does not set axis labels.
✗ Incorrect
set_ylabel() sets the label for the y-axis of the given axis object.
3fill in blank
hardFix the error in the code to correctly set the color of the second y-axis tick labels.
Matplotlib
ax2.tick_params(axis='y', [1]='b')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' or 'tickcolor' causes no effect or errors.
Misspelling 'labelcolor' as 'labelcolour'.
✗ Incorrect
The tick_params method uses 'labelcolor' to set the color of tick labels.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
Matplotlib
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word > 3' compares strings to numbers, causing errors.
Using 'word' instead of 'len(word)' for values.
✗ Incorrect
The dictionary comprehension maps each word to its length if the length is greater than 3.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 3.
Matplotlib
words = ['data', 'science', 'is', 'fun'] result = [1]: [2] for [3] in words if len([3]) > 3}}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len' as loop variable causes errors.
Not using upper() for keys.
Mismatching variable names in comprehension.
✗ Incorrect
The comprehension maps uppercase words to their lengths, iterating over each word and filtering by length.