0
0
Matplotlibdata~10 mins

Constrained layout vs tight layout in Matplotlib - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable tight layout in a matplotlib plot.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
plt.[1]()
Drag options to blanks, or click blank then click option'
Ashow
Bsavefig
Ctight_layout
Dfigure
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.show() instead of plt.tight_layout()
Calling tight_layout on the Axes object instead of pyplot
2fill in blank
medium

Complete the code to enable constrained layout when creating a figure.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots(constrained_layout=[1])
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()
Drag options to blanks, or click blank then click option'
ATrue
BFalse
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a boolean
Using False which disables constrained layout
3fill in blank
hard

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

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
fig.[1]()
Drag options to blanks, or click blank then click option'
Atight_layout
Bconstrained_layout
Csavefig
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Calling show() instead of tight_layout()
Trying to call constrained_layout() as a method
4fill in blank
hard

Fill both blanks to create a figure with constrained layout and plot a line.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots([1]=[2])
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()
Drag options to blanks, or click blank then click option'
Aconstrained_layout
BTrue
CFalse
Dtight_layout
Attempts:
3 left
💡 Hint
Common Mistakes
Using tight_layout as a parameter name
Setting constrained_layout to False
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters words by length and squares the length.

Matplotlib
words = ['cat', 'dog', 'elephant', 'bee']
lengths = {word: len(word)[1]2 for word in words if len(word) [2] 3 and word != [3]
print(lengths)
Drag options to blanks, or click blank then click option'
A**
B>
C'dog'
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '**' for power
Using '<' instead of '>' for filtering
Not quoting the word 'dog'