The plot call has only one list, so it treats it as y-values with x as indices 0,1,2.
Step 2: Understand matplotlib behavior
This is valid syntax; it plots y-values against default x-values. So no error here.
Step 3: Re-examine options carefully
The plot function is missing y-values says missing y-values, but y-values are given. The legend function is called before plot is wrong order. The label parameter is invalid in plot label is valid. There is no error; the code runs correctly says no error.
Final Answer:
There is no error; the code runs correctly -> Option D
Quick Check:
Code runs fine with legend after plot [OK]
Hint: Check if plot syntax matches matplotlib docs [OK]
Common Mistakes:
Assuming single list plot is invalid
Thinking legend must come before plot
Believing label is not accepted in plot
5. You want to create a scatter plot with blue triangles, add grid lines, and label axes as 'Height' and 'Weight'. Which code snippet correctly does this?
hard
A. plt.scatter(x, y, marker='s', color='red')
plt.grid(True)
plt.xlabel('Weight')
plt.ylabel('Height')
B. plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight')
C. plt.plot(x, y, marker='o', color='green')
plt.grid(False)
plt.xlabel('Weight')
plt.ylabel('Height')
D. plt.plot(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight')
Solution
Step 1: Identify scatter plot with blue triangles
Use plt.scatter() with marker='^' and color='blue'.
Step 2: Check grid and axis labels
Grid must be enabled with plt.grid(True), and axes labeled 'Height' and 'Weight' correctly.
Step 3: Match code snippet to requirements
plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight') matches all requirements exactly.
Final Answer:
plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight') -> Option B
Quick Check:
Scatter + blue triangles + grid + correct labels = plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight') [OK]