DP-500 · Question #8
You have the following Python code in an Apache Spark notebook. ``python import matplotlib.pyplot as plt import numpy as np ys = 100 * np.random.rand(100) x = [x for x in range(len(ys))] plt.plot(x…
The correct answer is D. an area chart. The Python code uses matplotlib.pyplot.plot for a line and matplotlib.pyplot.fill_between to color the area between the line and a constant, producing an area chart.
Question
import matplotlib.pyplot as plt
import numpy as np
ys = 100 * np.random.rand(100)
x = [x for x in range(len(ys))]
plt.plot(x, ys, '-r')
plt.fill_between(x, ys, 395, where=(ys > 395), facecolor='g', alpha=0.5)
plt.title("Chart Sample")
plt.show()
Which type of chart will the code produce?Exhibits
Options
- Aa stacked bar chart
- Ba pie chart
- Ca bar chart
- Dan area chart
How the community answered
(35 responses)- A3% (1)
- B3% (1)
- C6% (2)
- D89% (31)
Why each option
The Python code uses matplotlib.pyplot.plot for a line and matplotlib.pyplot.fill_between to color the area between the line and a constant, producing an area chart.
A stacked bar chart would typically involve plt.bar with a bottom parameter, or multiple plt.bar calls, which is not present here.
A pie chart is created using plt.pie(), which is not used in the provided code.
A bar chart is created using plt.bar(), which is not present in the provided code.
The plt.plot(x, ys, '-r') function creates a line plot, and crucially, plt.fill_between(x, ys, 395, ...) is specifically designed to fill the area between two curves or a curve and a horizontal line, which is the defining characteristic of an area chart.
Concept tested: Matplotlib chart types - area chart
Source: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.fill_between.html
Topics
Community Discussion
No community discussion yet for this question.

