nerdexam
Microsoft

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.

Explore and visualize data

Question

You have the following Python code in an Apache Spark notebook.
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

DP-500 question #8 exhibit 1
DP-500 question #8 exhibit 2

Options

  • Aa stacked bar chart
  • Ba pie chart
  • Ca bar chart
  • Dan area chart

How the community answered

(35 responses)
  • A
    3% (1)
  • B
    3% (1)
  • C
    6% (2)
  • D
    89% (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.

Aa stacked bar chart

A stacked bar chart would typically involve plt.bar with a bottom parameter, or multiple plt.bar calls, which is not present here.

Ba pie chart

A pie chart is created using plt.pie(), which is not used in the provided code.

Ca bar chart

A bar chart is created using plt.bar(), which is not present in the provided code.

Dan area chartCorrect

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

#matplotlib#data visualization#area chart#Python

Community Discussion

No community discussion yet for this question.

Full DP-500 Practice