0
0
Matplotlibdata~10 mins

LaTeX integration for papers 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 enable LaTeX rendering in matplotlib plots.

Matplotlib
import matplotlib.pyplot as plt
plt.rcParams['text.[1]'] = True
plt.plot([1, 2, 3], [1, 4, 9])
plt.title(r'$y = x^2$')
plt.show()
Drag options to blanks, or click blank then click option'
Ausetex
Blatex
Cmathtext
Dtextmode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text.latex' instead of 'text.usetex'.
Forgetting to set the parameter to True.
Trying to use 'mathtext' which is different from full LaTeX rendering.
2fill in blank
medium

Complete the code to set the font family to 'serif' for LaTeX text in matplotlib.

Matplotlib
import matplotlib.pyplot as plt
plt.rcParams['font.[1]'] = 'serif'
plt.rcParams['text.usetex'] = True
plt.title(r'$\alpha + \beta = \gamma$')
plt.show()
Drag options to blanks, or click blank then click option'
Astyle
Bvariant
Cweight
Dfamily
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'font.style' which controls italic or normal style.
Using 'font.weight' which controls boldness.
Using 'font.variant' which is less common.
3fill in blank
hard

Fix the error in the code to correctly display LaTeX math in the plot label.

Matplotlib
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.plot([1, 2, 3], [1, 4, 9])
plt.xlabel('$x^2$')
plt.ylabel(r'$[1]$')
plt.show()
Drag options to blanks, or click blank then click option'
A\sqrt{x}
Bsqrt{x}
C\frac{1}{x}
Dfrac{1}{x}
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the backslash before LaTeX commands.
Not using raw strings when needed.
Using incomplete LaTeX syntax.
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', 'ai', 'ml']
lengths = { [1] : len([2]) for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Bwords
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list name 'words' instead of the loop variable.
Using a fixed string like 'data' instead of the variable.
Mixing key and value variables.
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 2.

Matplotlib
words = ['cat', 'dog', 'a', 'elephant']
result = { [1] : [2] for word in words if len(word) [3] 2 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Using wrong comparison operators.
Mixing up key and value expressions.