B. The code is correct and will display a blue bar chart with legend and labels.
C. The plt.show() function is missing.
D. The label argument is missing from plt.legend().
Solution
Step 1: Check plt.bar() arguments
The color and label arguments are valid for plt.bar() to set bar color and legend label.
Step 2: Verify legend and labels usage
plt.legend() shows the legend using the label provided. Labels for axes are set correctly.
Final Answer:
The code is correct and will display a blue bar chart with legend and labels. -> Option B
Quick Check:
Valid arguments and legend usage = D [OK]
Hint: Remember plt.bar() accepts color and label arguments [OK]
Common Mistakes:
Thinking color is invalid for bar charts
Expecting label in plt.legend() call
Forgetting plt.show() to display plot
5. You want to create a line chart showing monthly sales with a green dashed line, thicker width, and clear axis labels. Which code snippet achieves this best?
B. plt.plot(months, sales, color='blue', linestyle='-', linewidth=1)
plt.xlabel('Sales')
plt.ylabel('Month')
plt.show()
C. plt.plot(months, sales, linestyle='--')
plt.title('Sales')
plt.show()
D. plt.plot(months, sales, color='green', linewidth=1)
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
Solution
Step 1: Check line style and color requirements
plt.plot(months, sales, color='green', linestyle='--', linewidth=3)
plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Monthly Sales')
plt.show() uses color='green' and linestyle='--' for a green dashed line, matching the requirement.
Step 2: Verify line thickness and labels
plt.plot(months, sales, color='green', linestyle='--', linewidth=3)
plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Monthly Sales')
plt.show() sets linewidth=3 for thicker line and adds clear axis labels and title.
Step 3: Compare with other options
Options B, C, and D either use wrong colors, missing labels, or incorrect line styles.
Final Answer:
plt.plot(months, sales, color='green', linestyle='--', linewidth=3)\nplt.xlabel('Month')\nplt.ylabel('Sales')\nplt.title('Monthly Sales')\nplt.show() -> Option A
Quick Check:
Green dashed thick line with labels = A [OK]
Hint: Match all style and label requirements carefully [OK]
Common Mistakes:
Ignoring line style or color requirements
Swapping axis labels
Using default line thickness
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.