0
0
Matplotlibdata~10 mins

Secondary axes 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 secondary y-axis on the right side of the plot.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
secax = ax.[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Asecondary_yaxis
Bsecondary_xaxis
Ctwiny
Dtwinx
Attempts:
3 left
💡 Hint
Common Mistakes
Using secondary_xaxis instead of twinx
Using twiny which creates a secondary x-axis
Using secondary_yaxis which returns an Axis object unsuitable for plotting
2fill in blank
medium

Complete the code to set the label of the secondary y-axis.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
secax = ax.twinx()
secax.[1]("Secondary Y")
plt.show()
Drag options to blanks, or click blank then click option'
Aset_xlabel
Bset_ylabel
Cset_title
Dset_label
Attempts:
3 left
💡 Hint
Common Mistakes
Using set_xlabel which sets the x-axis label
Using set_title which sets the plot title
Using set_label which is not a method of Axes
3fill in blank
hard

Fix the error in the code to correctly create a secondary x-axis at the top.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
secax = ax.[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Atwiny
Btwinx
Csecondary_xaxis
Dsecondary_yaxis
Attempts:
3 left
💡 Hint
Common Mistakes
Using secondary_xaxis which returns an Axis object unsuitable for plotting
Using twinx which creates a secondary y-axis
Using secondary_yaxis which is for y-axis
4fill in blank
hard

Fill both blanks to create a secondary y-axis and set its label.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
secax = ax.[1]()
secax.[2]("Temperature (°C)")
plt.show()
Drag options to blanks, or click blank then click option'
Atwinx
Bset_ylabel
Cset_xlabel
Dtwiny
Attempts:
3 left
💡 Hint
Common Mistakes
Using twiny instead of twinx
Using set_xlabel to label y-axis
Forgetting to create the secondary axis before labeling
5fill in blank
hard

Fill all three blanks to create a secondary x-axis at the top, set its label, and plot data on it.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
secax = ax.[1]()
secax.[2]("Time (s)")
secax.[3]([0, 1, 2], [10, 20, 30])
plt.show()
Drag options to blanks, or click blank then click option'
Atwinx
Bset_xlabel
Cplot
Dtwiny
Attempts:
3 left
💡 Hint
Common Mistakes
Using twinx which creates a secondary y-axis
Using set_ylabel instead of set_xlabel for x-axis label
Forgetting to plot data on the secondary axis