nerdexam
Microsoft

DP-500 · Question #54

You have the following code in an Azure Synapse notebook: ``python import matplotlib.pyplot as plt x1 = [1, 3, 4, 5, 6, 7, 9] y1 = [4, 7, 2, 4, 7, 8, 3] x2 = [2, 4, 6, 8, 10] y2 = [5, 6, 2, 6, 2]…

This question tests understanding of Python's matplotlib library within Azure Synapse notebooks, specifically how bar charts are constructed, labeled, and rendered when multiple datasets are plotted on the same axes.

Explore and visualize data

Question

You have the following code in an Azure Synapse notebook:
import matplotlib.pyplot as plt
x1 = [1, 3, 4, 5, 6, 7, 9]
y1 = [4, 7, 2, 4, 7, 8, 3]
x2 = [2, 4, 6, 8, 10]
y2 = [5, 6, 2, 6, 2]
plt.bar(x1, y1, label='Blue Item', color='b')
plt.bar(x2, y2, label='Green Item', color='g')
plt.plot()
plt.xlabel("Number")
plt.ylabel("Height")
plt.title("My Chart")
plt.legend()
plt.show()
Use the drop-down menus to select the answer choice that completes each statement based on the information presented in the code. NOTE: Each correct selection is worth one point.

Explanation

This question tests understanding of Python's matplotlib library within Azure Synapse notebooks, specifically how bar charts are constructed, labeled, and rendered when multiple datasets are plotted on the same axes.

Approach. The code creates a grouped bar chart with two datasets using plt.bar(). The first dataset (x1/y1) renders blue bars labeled 'Blue Item', and the second (x2/y2) renders green bars labeled 'Green Item' - both on the same chart. The x-axis is labeled 'Number', the y-axis is labeled 'Height', and the chart title is 'My Chart'. Because x1 and x2 have non-overlapping values, the bars appear side by side across the x-axis rather than stacked or truly grouped. The plt.legend() call displays a legend with both color-coded entries ('Blue Item' and 'Green Item'), and plt.show() renders the final visualization. Any drop-down asking about chart type (bar), axis labels (Number/Height), title (My Chart), number of data series (two), or legend entries (Blue Item, Green Item) should be answered using these literal values from the code.

Concept tested. Reading and interpreting matplotlib bar chart code: identifying chart type (plt.bar), axis labels (plt.xlabel/plt.ylabel), chart title (plt.title), legend behavior (plt.legend with per-series labels), and how multiple plt.bar() calls overlay on the same axes within an Azure Synapse (Spark) notebook environment.

Reference. Microsoft Learn - Visualize data with matplotlib in Azure Synapse Analytics; matplotlib.org - matplotlib.pyplot.bar documentation

Topics

#Matplotlib#Data Visualization#Bar Chart#Python Programming

Community Discussion

No community discussion yet for this question.

Full DP-500 Practice