A diverging bar chart is a type of bar chart that shows positive and negative values extending in opposite directions from a central axis. It helps compare two groups or show how values differ from a midpoint.
Click to reveal answer
beginner
Why use a diverging bar chart instead of a regular bar chart?
Diverging bar charts clearly show differences between positive and negative values, making it easy to see which values are above or below a baseline. Regular bar charts do not emphasize this contrast as well.
Click to reveal answer
beginner
Which matplotlib function is commonly used to create bar charts?
The function plt.bar() is commonly used to create bar charts in matplotlib.
Click to reveal answer
intermediate
How do you center bars around zero in a diverging bar chart?
You plot bars with positive values extending to the right and negative values extending to the left from the zero line on the x-axis, so zero is the center point.
Click to reveal answer
beginner
What is a practical example of using a diverging bar chart?
A diverging bar chart can show survey results where responses range from negative to positive opinions, helping visualize how many people feel positively or negatively about a topic.
Click to reveal answer
What does a diverging bar chart best show?
ATime series trends
BOnly positive values
COnly negative values
DDifferences between positive and negative values
✗ Incorrect
Diverging bar charts highlight differences between positive and negative values around a central zero line.
Which matplotlib function is used to create bars in a bar chart?
Aplt.plot()
Bplt.bar()
Cplt.scatter()
Dplt.hist()
✗ Incorrect
The plt.bar() function creates bar charts in matplotlib.
In a diverging bar chart, where do negative values extend?
ATo the left from zero
BUpwards from zero
CTo the right from zero
DDownwards from zero
✗ Incorrect
Negative values extend to the left from the zero line in a diverging bar chart.
What is a key benefit of diverging bar charts?
AThey show only totals
BThey show time trends
CThey emphasize differences from a midpoint
DThey replace pie charts
✗ Incorrect
Diverging bar charts emphasize how values differ from a central midpoint, usually zero.
Which axis usually represents the zero baseline in a diverging bar chart?
AX-axis
BZ-axis
CY-axis
DColor axis
✗ Incorrect
The x-axis usually represents the zero baseline from which bars diverge left or right.
Explain how to create a diverging bar chart using matplotlib.
Think about how bars extend from zero in opposite directions.
You got /5 concepts.
Describe a real-life situation where a diverging bar chart is useful.
Consider opinions or ratings that can be positive or negative.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a diverging bar chart in data visualization?
easy
A. To visualize hierarchical data using nested bars
B. To show positive and negative values side by side for easy comparison
C. To plot time series data with a line connecting points
D. To display only positive values in a stacked format
Solution
Step 1: Understand the chart type
Diverging bar charts are designed to display data that has both positive and negative values.
Step 2: Identify the purpose
They place positive and negative bars side by side to highlight differences clearly.
Final Answer:
To show positive and negative values side by side for easy comparison -> Option B
Quick Check:
Diverging bar chart = positive & negative side by side [OK]
Hint: Look for charts showing positive and negative bars side by side [OK]
Common Mistakes:
Confusing diverging bar charts with stacked bar charts
Thinking they only show positive values
Mixing up with line charts
2. Which of the following is the correct way to create a diverging bar chart using matplotlib's barh function for negative and positive values?
easy
A. plt.barh(y_pos, abs(values), color='red')
B. plt.barh(y_pos, values, color='blue')
C. plt.barh(y_pos, values, color=['red' if v < 0 else 'green' for v in values])
D. plt.barh(y_pos, values, color=['green' if v > 0 else 'blue' for v in values])
Solution
Step 1: Understand color assignment for diverging bars
Colors should differ based on whether values are positive or negative to highlight divergence.
Step 2: Check the color list comprehension
plt.barh(y_pos, values, color=['red' if v < 0 else 'green' for v in values]) uses a list comprehension assigning 'red' for negative and 'green' for positive values, which is correct.
Final Answer:
plt.barh(y_pos, values, color=['red' if v < 0 else 'green' for v in values]) -> Option C
Quick Check:
Color by sign = plt.barh(y_pos, values, color=['red' if v < 0 else 'green' for v in values]) [OK]
Hint: Use list comprehension to color bars by sign [OK]
Common Mistakes:
Using a single color for all bars
Taking absolute values which lose sign info
Assigning wrong colors to positive/negative values
3. Given the code below, what will be the color of the bar corresponding to the value -3?
import matplotlib.pyplot as plt
values = [4, -3, 2, -1]
y_pos = range(len(values))
colors = ['green' if v > 0 else 'red' for v in values]
plt.barh(y_pos, values, color=colors)
plt.show()
medium
A. No color (default)
B. Green
C. Blue
D. Red
Solution
Step 1: Analyze the color assignment logic
The list comprehension assigns 'green' if value > 0, else 'red'.
Step 2: Check the value -3
-3 is less than 0, so it gets 'red'.
Final Answer:
Red -> Option D
Quick Check:
Negative value color = red [OK]
Hint: Negative values get 'red' color here [OK]
Common Mistakes:
Assuming negative values get green
Confusing color assignment logic
Ignoring the else condition in list comprehension
4. The following code is intended to create a diverging bar chart but does not color bars correctly. What is the error?
values = [5, -2, 3, -4]
y_pos = range(len(values))
colors = ['green' if v < 0 else 'red' for v in values]
plt.barh(y_pos, values, color=colors)
plt.show()
medium
A. The color condition is reversed; positive values should be green
B. The y_pos variable should be a list, not a range
C. The barh function cannot accept a color list
D. Values should be absolute for coloring
Solution
Step 1: Review the color condition
The code assigns 'green' if value < 0, else 'red'. This means negative values are green and positive are red.
Step 2: Understand diverging bar chart color convention
Usually, positive values are green and negative values are red to show gains and losses clearly.
Final Answer:
The color condition is reversed; positive values should be green -> Option A
Quick Check:
Positive = green, negative = red [OK]
Hint: Positive bars usually green, negative bars red [OK]
Common Mistakes:
Reversing color conditions
Thinking range is invalid for y_pos
Using absolute values for coloring
5. You have a dataset with sales gains and losses for 5 products: {'A': 10, 'B': -5, 'C': 0, 'D': 7, 'E': -3}. How would you create a diverging bar chart that clearly shows gains in green, losses in red, and zero values in gray using matplotlib?
hard
A. Use a color list with a condition: green if >0, red if <0, gray if 0, then plot with plt.barh
B. Plot absolute values only with a single color to avoid confusion
C. Use plt.barh with colors assigned randomly
D. Plot only positive values and ignore zeros and negatives
Solution
Step 1: Define color conditions for all cases
Assign 'green' for positive, 'red' for negative, and 'gray' for zero values to distinguish all clearly.
Step 2: Create color list and plot
Use a list comprehension to assign colors based on value sign, then plot with plt.barh using these colors.
Final Answer:
Use a color list with a condition: green if >0, red if <0, gray if 0, then plot with plt.barh -> Option A
Quick Check:
Color by sign including zero = Use a color list with a condition: green if >0, red if <0, gray if 0, then plot with plt.barh [OK]
Hint: Add gray for zero values in color list [OK]
Common Mistakes:
Ignoring zero values color
Using absolute values losing sign info
Assigning random colors without logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.