0
0
Matplotlibdata~10 mins

Dual y-axis for different scales 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 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'
Asubplots
Btwiny
Csubplot
Dtwinx
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.
2fill in blank
medium

Complete 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'
Aset_ylabel
Bset_xlabel
Clabel
Dset_title
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.
3fill in blank
hard

Fix 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'
Alabelcolour
Bcolor
Clabelcolor
Dtickcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' or 'tickcolor' causes no effect or errors.
Misspelling 'labelcolor' as 'labelcolour'.
4fill in blank
hard

Fill 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'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word > 3' compares strings to numbers, causing errors.
Using 'word' instead of 'len(word)' for values.
5fill in blank
hard

Fill 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'
Aword.upper()
Blen(word)
Cword
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len' as loop variable causes errors.
Not using upper() for keys.
Mismatching variable names in comprehension.