What will be the output of this Python code using matplotlib to create a diverging bar chart?
import matplotlib.pyplot as plt import numpy as np labels = ['A', 'B', 'C', 'D'] values = np.array([3, -2, 5, -1]) colors = ['green' if x > 0 else 'red' for x in values] plt.bar(labels, values, color=colors) plt.axhline(0, color='black') plt.show()
Look at how colors are assigned based on positive or negative values and the use of axhline.
The code creates a bar chart where positive values are green bars going upwards, negative values are red bars going downwards, and a black horizontal line at zero separates positive and negative bars.
Given the following data and code for a diverging bar chart, how many bars will be above zero and how many below zero?
import numpy as np values = np.array([10, -5, 0, 3, -7, 2])
Count values strictly greater than zero and strictly less than zero. Zero is neither.
Values above zero are 10, 3, and 2 (3 bars). Values below zero are -5 and -7 (2 bars). Zero is not counted.
Which of the following code snippets produces a diverging bar chart with labels on the y-axis and horizontal bars diverging from zero?
Think about which function creates horizontal bars and where the zero line should be.
plt.barh creates horizontal bars. The zero line for horizontal bars is vertical, so axvline is used. This matches option B.
What error will this code raise?
import matplotlib.pyplot as plt values = [4, -3, 2, -1] colors = ['green' if x > 0 else 'red' for x in values] plt.bar(range(len(values)), values, color=colors) plt.axhline(0, color='black') plt.show()
Check if the colors list length matches the number of bars and if imports are correct.
The code runs correctly because the colors list matches the number of bars, and plt is imported. Using range(len(values)) for x-axis is valid.
Given the following data representing changes in sales (positive and negative), what are the total positive and total negative sums?
sales_changes = [15, -7, 0, 8, -3, -5, 12]
Add only positive numbers for positive sum, and only negative numbers for negative sum.
Positive values: 15 + 8 + 12 = 35 is correct because 0 is not positive. Negative values: -7 + -3 + -5 = -15. So total positive sum is 35, total negative sum is -15. Option C says 27 positive sum, which is incorrect. So correct is option C.