0
0
Matplotlibdata~10 mins

Tight layout for spacing 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 apply tight layout to the plot.

Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.[1]()  # Adjust spacing
plt.show()
Drag options to blanks, or click blank then click option'
Ashow
Bfigure
Cplot
Dtight_layout
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.show() instead of plt.tight_layout()
Forgetting to call any layout adjustment function
2fill in blank
medium

Complete the code to create a figure and apply tight layout.

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [3, 2, 1])
fig.[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Atight_layout
Bsavefig
Cshow
Dsubplots_adjust
Attempts:
3 left
💡 Hint
Common Mistakes
Calling plt.tight_layout() instead of fig.tight_layout()
Using subplots_adjust without parameters
3fill in blank
hard

Fix the error in the code to correctly apply tight layout.

Matplotlib
import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 1)
axs[0].plot([1, 2, 3], [1, 4, 9])
axs[1].plot([1, 2, 3], [9, 4, 1])
plt.[1]()  # Missing parentheses
plt.show()
Drag options to blanks, or click blank then click option'
Atight_layout
Btight_layout()
Cshow()
Dfigure()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after tight_layout
Calling show() instead of tight_layout()
4fill in blank
hard

Fill both blanks to create two subplots and apply tight layout.

Matplotlib
import matplotlib.pyplot as plt

fig, [1] = plt.subplots(1, 2)
[2].plot([1, 2, 3], [3, 2, 1])
fig.tight_layout()
plt.show()
Drag options to blanks, or click blank then click option'
Aaxs
Baxs[0]
Cfig
Dplt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ax' for multiple subplots
Plotting on 'fig' instead of an axis
5fill in blank
hard

Fill all three blanks to create a figure with 3 subplots, plot on the second, and apply tight layout.

Matplotlib
import matplotlib.pyplot as plt

fig, [1] = plt.subplots(3, 1)
[2].plot([1, 2, 3], [2, 4, 6])
fig.[3]()
plt.show()
Drag options to blanks, or click blank then click option'
Aaxes
Baxes[1]
Ctight_layout
Dplot
Attempts:
3 left
💡 Hint
Common Mistakes
Plotting on axes[0] instead of axes[1]
Forgetting to call tight_layout() as a function
Using 'ax' instead of 'axes' for multiple subplots