Bird
Raised Fist0
Matplotlibdata~5 mins

Horizontal grouped bars in Matplotlib - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a horizontal grouped bar chart?
A horizontal grouped bar chart shows bars side-by-side along the horizontal axis. Each group represents a category, and bars within a group represent sub-categories. It helps compare multiple groups across categories.
Click to reveal answer
intermediate
How do you control the position of bars in a horizontal grouped bar chart in matplotlib?
You control bar positions by setting the y-values with an offset for each group. Use numpy arrays and add/subtract a fixed height to shift bars side-by-side vertically.
Click to reveal answer
beginner
Which matplotlib function is used to create horizontal bars?
The function plt.barh() creates horizontal bars in matplotlib.
Click to reveal answer
beginner
Why use horizontal grouped bars instead of vertical grouped bars?
Horizontal bars are easier to read when category names are long or when you want to emphasize comparison across categories horizontally.
Click to reveal answer
intermediate
What parameters are important to set for clear horizontal grouped bars?
Important parameters include bar height (height in barh), y positions with offsets, colors for groups, and labels for clarity.
Click to reveal answer
Which matplotlib function creates horizontal bars?
Aplt.plot()
Bplt.bar()
Cplt.barh()
Dplt.scatter()
In a horizontal grouped bar chart, how do you place bars side-by-side?
ABy changing the y positions with offsets
BBy changing the x positions with offsets
CBy changing the bar colors
DBy changing the bar heights
What does each group in a grouped bar chart represent?
AThe color of the bars
BA single data point
CThe x-axis label
DA category with multiple sub-categories
Why might you choose horizontal bars over vertical bars?
ATo save vertical space
BTo better display long category names
CBecause horizontal bars are faster to plot
DBecause vertical bars cannot be grouped
Which parameter controls the thickness of bars in plt.barh()?
Aheight
Bwidth
Csize
Dlength
Explain how to create a horizontal grouped bar chart using matplotlib.
Think about how to position bars side-by-side horizontally.
You got /5 concepts.
    Describe when and why you would use horizontal grouped bars instead of vertical grouped bars.
    Consider the visual clarity and label length.
    You got /4 concepts.

      Practice

      (1/5)
      1.

      What is the main purpose of using horizontal grouped bars in a chart?

      easy
      A. To compare multiple categories side by side horizontally
      B. To show data as a pie chart
      C. To display data only vertically
      D. To create a line graph

      Solution

      1. Step 1: Understand the chart type

        Horizontal grouped bars place bars side by side horizontally to compare groups.
      2. Step 2: Identify the purpose

        This layout helps compare multiple categories clearly across groups.
      3. Final Answer:

        To compare multiple categories side by side horizontally -> Option A
      4. Quick Check:

        Horizontal grouped bars = compare categories horizontally [OK]
      Hint: Grouped bars show side-by-side comparisons horizontally [OK]
      Common Mistakes:
      • Confusing horizontal bars with pie charts
      • Thinking grouped bars are vertical only
      • Mixing line graphs with bar charts
      2.

      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()
      easy
      A. Use plt.scatter for grouped bars
      B. Use plt.bar with shifted positions for each group
      C. Use plt.plot with x and y values
      D. Use plt.barh with shifted positions for each group

      Solution

      1. Step 1: Check the function used

        The code uses plt.barh which creates horizontal bars.
      2. Step 2: Verify grouping method

        Positions are shifted by adding width to x to group bars side by side horizontally.
      3. Final Answer:

        Use plt.barh with shifted positions for each group -> Option D
      4. Quick Check:

        plt.barh + shifted positions = grouped horizontal bars [OK]
      Hint: Use plt.barh and shift positions to group bars horizontally [OK]
      Common Mistakes:
      • 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
      3.

      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()
      medium
      A. A line plot with two lines
      B. Two vertical bar groups overlapping
      C. Two sets of horizontal bars side by side for each label
      D. A scatter plot with points at x positions

      Solution

      1. Step 1: Analyze barh usage

        plt.barh creates horizontal bars at positions x and x + width, shifted to group bars.
      2. Step 2: Check y-axis labels

        yticks are set at middle positions between bars for labels 'X' and 'Y'.
      3. Final Answer:

        Two sets of horizontal bars side by side for each label -> Option C
      4. Quick Check:

        barh + shifted positions + ytick labels = grouped horizontal bars [OK]
      Hint: Shift y positions and set yticks for grouped horizontal bars [OK]
      Common Mistakes:
      • Confusing horizontal bars with vertical bars
      • Not setting yticks causing label misalignment
      • Expecting line or scatter plot output
      4.

      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()
      medium
      A. Bars overlap because positions are not shifted for grouping
      B. Height parameter is missing
      C. yticks are set incorrectly
      D. Values lists have different lengths

      Solution

      1. Step 1: Check bar positions

        Both plt.barh calls use the same x positions, so bars overlap instead of grouping.
      2. Step 2: Verify grouping method

        Positions should be shifted by width to separate groups horizontally.
      3. Final Answer:

        Bars overlap because positions are not shifted for grouping -> Option A
      4. Quick Check:

        Same x positions without shift = overlapping bars [OK]
      Hint: Shift bar positions to avoid overlap in grouped bars [OK]
      Common Mistakes:
      • Not shifting positions for second group
      • Incorrect yticks placement
      • Assuming height parameter fixes overlap
      5.

      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?

      hard
      A. Use plt.plot to draw lines for each product
      B. Use plt.barh with positions shifted by width and set yticks at x + width/2
      C. Use plt.barh with same positions for both years and set yticks at x
      D. Use plt.bar with positions shifted by width and set xticks at x + width/2

      Solution

      1. Step 1: Use plt.barh with shifted positions

        Shift positions by width for each year to group bars horizontally side by side.
      2. Step 2: Set yticks at middle of grouped bars

        Set yticks at x + width/2 so product labels appear centered between bars.
      3. Final Answer:

        Use plt.barh with positions shifted by width and set yticks at x + width/2 -> Option B
      4. Quick Check:

        Shift positions + yticks center = clear grouped horizontal bars [OK]
      Hint: Shift bars and center yticks for clear grouped horizontal bars [OK]
      Common Mistakes:
      • Using vertical bars instead of horizontal
      • Not shifting positions causing overlap
      • Setting yticks incorrectly causing label misalignment
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard