Complete the code to create a figure with 2 subplots using matplotlib.
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, [1]) plt.show()
The plt.subplots function creates a figure and a set of subplots. Here, 1, 2 means 1 row and 2 columns, so 2 subplots.
Complete the code to set the title of the first subplot to 'First Plot'.
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 2) axs[[1]].set_title('First Plot') plt.show()
Subplots are indexed starting at 0. The first subplot is axs[0].
Fix the error in the code to set the x-label of the second subplot to 'X Axis'.
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 2) axs.[1](1).set_xlabel('X Axis') plt.show()
To access the second subplot, use axs[1]. The __getitem__ method is called when using square brackets.
Fill both blanks to set the y-label of the first subplot to 'Y Axis' and change its background color to light gray.
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 2) axs[[1]].set_ylabel('Y Axis') axs[[2]].set_facecolor('lightgray') plt.show()
Both actions target the first subplot, which is at index 0.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary comprehension syntax is {key: value for item in iterable if condition}. Here, key is word, value is len(word), and the condition is len(word) > 3.