Look at this code that uses matplotlib.pyplot. What will it show?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.title('Simple Line') plt.xlabel('X axis') plt.ylabel('Y axis') plt.show()
Remember, plt.plot() draws lines connecting points.
The plt.plot() function draws a line graph connecting the points given by the x and y lists. The labels and title appear as expected.
Count the number of lines that will appear after running this code.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) plt.show()
Each plt.plot() call adds one line.
There are two plt.plot() calls, so two lines will be drawn on the same plot.
Examine the code below. What kind of plot will it produce?
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) plt.plot(x, y) plt.fill_between(x, y, color='skyblue', alpha=0.5) plt.title('Sine Wave with Fill') plt.show()
fill_between colors the area under the curve.
The code plots a sine wave line and shades the area under it with a semi-transparent blue color.
Look at this code snippet. What error will it cause when run?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5]) plt.show()
Check if the x and y lists have the same length.
The x list has 3 elements but y has only 2, so plt.plot() raises a ValueError about dimension mismatch.
You want to create two plots side by side using Pyplot. Which code will do this correctly?
Use plt.subplots(rows, columns) to create a grid of plots.
Option D correctly creates a figure with 1 row and 2 columns of subplots, then plots on each axis separately.