Horizontal grouped bars help compare different groups side by side using horizontal bars. They make it easy to see differences across categories.
Horizontal grouped bars in Matplotlib
import matplotlib.pyplot as plt labels = ['Group1', 'Group2', 'Group3'] values1 = [10, 20, 30] values2 = [15, 25, 35] import numpy as np bar_width = 0.35 indices = np.arange(len(labels)) plt.barh(indices, values1, bar_width, label='Set 1') plt.barh(indices + bar_width, values2, bar_width, label='Set 2') plt.yticks(indices + bar_width / 2, labels) plt.legend() plt.show()
Use barh() to create horizontal bars.
Shift the bars on the y-axis by bar width to group them side by side.
plt.barh([0, 1, 2], [5, 10, 15], 0.4, label='A') plt.barh([0.4, 1.4, 2.4], [7, 12, 17], 0.4, label='B') plt.yticks([0.2, 1.2, 2.2], ['X', 'Y', 'Z']) plt.legend() plt.show()
labels = ['Apples', 'Bananas'] values1 = [30, 40] values2 = [35, 45] indices = np.arange(len(labels)) plt.barh(indices, values1, 0.3, label='2019') plt.barh(indices + 0.3, values2, 0.3, label='2020') plt.yticks(indices + 0.15, labels) plt.legend() plt.show()
This program shows student scores in three subjects for two years using horizontal grouped bars.
import matplotlib.pyplot as plt import numpy as np labels = ['Math', 'Science', 'English'] scores_2023 = [88, 92, 85] scores_2024 = [90, 95, 87] bar_width = 0.35 indices = np.arange(len(labels)) plt.barh(indices, scores_2023, bar_width, label='2023') plt.barh(indices + bar_width, scores_2024, bar_width, label='2024') plt.yticks(indices + bar_width / 2, labels) plt.xlabel('Scores') plt.title('Student Scores by Subject and Year') plt.legend() plt.tight_layout() plt.show()
Make sure to shift the bars on the y-axis by the bar width to avoid overlap.
Use plt.yticks() to set the category labels in the middle of grouped bars.
Horizontal bars are useful when category names are long or many.
Horizontal grouped bars show side-by-side comparisons using horizontal bars.
Use barh() and shift bars on y-axis to group them.
Set y-axis ticks to label groups clearly.
Practice
What is the main purpose of using horizontal grouped bars in a chart?
Solution
Step 1: Understand the chart type
Horizontal grouped bars place bars side by side horizontally to compare groups.Step 2: Identify the purpose
This layout helps compare multiple categories clearly across groups.Final Answer:
To compare multiple categories side by side horizontally -> Option AQuick Check:
Horizontal grouped bars = compare categories horizontally [OK]
- Confusing horizontal bars with pie charts
- Thinking grouped bars are vertical only
- Mixing line graphs with bar charts
Which of the following is the correct way to create horizontal grouped bars in matplotlib?
import matplotlib.pyplot as plt
import numpy as np
labels = ['A', 'B', 'C']
values1 = [5, 7, 3]
values2 = [6, 2, 4]
x = np.arange(len(labels))
width = 0.35
plt.barh(x, values1, height=width)
plt.barh(x + width, values2, height=width)
plt.show()Solution
Step 1: Check the function used
The code uses plt.barh which creates horizontal bars.Step 2: Verify grouping method
Positions are shifted by adding width to x to group bars side by side horizontally.Final Answer:
Use plt.barh with shifted positions for each group -> Option DQuick Check:
plt.barh + shifted positions = grouped horizontal bars [OK]
- Using plt.bar instead of plt.barh for horizontal bars
- Not shifting positions causing overlapping bars
- Using plt.plot or plt.scatter which are not bar charts
What will be the output of the following code?
import matplotlib.pyplot as plt
import numpy as np
labels = ['X', 'Y']
values1 = [3, 8]
values2 = [5, 2]
x = np.arange(len(labels))
width = 0.4
plt.barh(x, values1, height=width, color='blue')
plt.barh(x + width, values2, height=width, color='red')
plt.yticks(x + width / 2, labels)
plt.show()Solution
Step 1: Analyze barh usage
plt.barh creates horizontal bars at positions x and x + width, shifted to group bars.Step 2: Check y-axis labels
yticks are set at middle positions between bars for labels 'X' and 'Y'.Final Answer:
Two sets of horizontal bars side by side for each label -> Option CQuick Check:
barh + shifted positions + ytick labels = grouped horizontal bars [OK]
- Confusing horizontal bars with vertical bars
- Not setting yticks causing label misalignment
- Expecting line or scatter plot output
Identify the error in this code for horizontal grouped bars:
import matplotlib.pyplot as plt
import numpy as np
labels = ['P', 'Q', 'R']
values1 = [4, 6, 5]
values2 = [7, 3, 8]
x = np.arange(len(labels))
width = 0.3
plt.barh(x, values1, height=width)
plt.barh(x, values2, height=width)
plt.yticks(x + width / 2, labels)
plt.show()Solution
Step 1: Check bar positions
Both plt.barh calls use the same x positions, so bars overlap instead of grouping.Step 2: Verify grouping method
Positions should be shifted by width to separate groups horizontally.Final Answer:
Bars overlap because positions are not shifted for grouping -> Option AQuick Check:
Same x positions without shift = overlapping bars [OK]
- Not shifting positions for second group
- Incorrect yticks placement
- Assuming height parameter fixes overlap
You have sales data for 3 products over 2 years:
products = ['A', 'B', 'C']
year1 = [10, 15, 7]
year2 = [12, 9, 14]How would you create a horizontal grouped bar chart to compare sales by product and year, ensuring product labels appear centered between grouped bars?
Solution
Step 1: Use plt.barh with shifted positions
Shift positions by width for each year to group bars horizontally side by side.Step 2: Set yticks at middle of grouped bars
Set yticks at x + width/2 so product labels appear centered between bars.Final Answer:
Use plt.barh with positions shifted by width and set yticks at x + width/2 -> Option BQuick Check:
Shift positions + yticks center = clear grouped horizontal bars [OK]
- Using vertical bars instead of horizontal
- Not shifting positions causing overlap
- Setting yticks incorrectly causing label misalignment
Ready to prove it?
Pass the test and this pattern moves unknown
