Complete the code to enable LaTeX rendering in matplotlib plots.
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()
Setting plt.rcParams['text.usetex'] = True tells matplotlib to use LaTeX for rendering text.
Complete the code to set the font family to 'serif' for LaTeX text in matplotlib.
import matplotlib.pyplot as plt plt.rcParams['font.[1]'] = 'serif' plt.rcParams['text.usetex'] = True plt.title(r'$\alpha + \beta = \gamma$') plt.show()
Setting plt.rcParams['font.family'] = 'serif' changes the font family to serif for LaTeX text.
Fix the error in the code to correctly display LaTeX math in the plot label.
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()
LaTeX commands require a backslash. So \sqrt{x} is correct, while missing backslash causes errors.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['data', 'science', 'ai', 'ml'] lengths = { [1] : len([2]) for word in words if len(word) > 3 }
The dictionary comprehension uses word as key and len(word) as value.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 2.
words = ['cat', 'dog', 'a', 'elephant'] result = { [1] : [2] for word in words if len(word) [3] 2 }
The comprehension maps word.upper() to len(word) for words longer than 2 characters.