Bar charts - horizontal

Matplotlib Basics

1 min read

Published Oct 5 2025


15
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonVisualisation

The barh() function creates horizontal bars.


Syntax:

plt.barh(y, width, height=0.8, left=None, color=None, edgecolor=None, label=None, alpha=1.0)

Parameters:

  • y → category positions (or names)
  • width → bar lengths (data values)
  • height → bar thickness
  • left → starting x-position (for stacking)
  • color → bar fill colour
  • edgecolor → colour of the bar border
  • label → legend label
  • alpha → transparency (0 to 1)



Basic horizontal bar chart example

import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries', 'Dates']
values = [25, 40, 15, 30]

plt.barh(categories, values)
plt.title("Basic Horizontal Bar Chart")
plt.xlabel("Quantity")
plt.ylabel("Fruit Type")
plt.show()

matplotlib bar chart horizontal basic example




Bar colours

import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries', 'Dates']
values = [25, 40, 15, 30]

plt.barh(categories, values, color=['lightcoral', 'yellow', 'lightgreen', 'lightblue'])
plt.title("Coloured Bars")
plt.show()

matplotlib bar chart horizontal colours example




Edge colour and line width

import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries', 'Dates']
values = [25, 40, 15, 30]

plt.barh(categories, values, color='lightblue', edgecolor='black', linewidth=2)
plt.title("Horizontal Bars with Borders")
plt.show()

matplotlib bar chart horizontal edge colours example




Bar height and offset

import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries', 'Dates']
values = [25, 40, 15, 30]

plt.barh(categories, values, height=0.5, left=5, color='cyan')
plt.title("Adjusted Height and Left Offset")
plt.show()

matplotlib bar chart horizontal height left example




Add labels on bars

import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries', 'Dates']
values = [25, 40, 15, 30]

bars = plt.barh(categories, values, color='lightgreen')
plt.title("Horizontal Bar Chart with Labels")

for bar in bars:
    plt.text(bar.get_width() + 1, bar.get_y() + bar.get_height()/2,
             str(bar.get_width()), va='center')

plt.show()

matplotlib bar chart horizontal bar labels example




Grouped horizontal bar chart

import matplotlib.pyplot as plt
import numpy as np

y = np.arange(4)
values_2024 = [25, 40, 15, 30]
values_2025 = [30, 35, 20, 25]

plt.barh(y - 0.2, values_2024, height=0.4, label='2024', color='skyblue')
plt.barh(y + 0.2, values_2025, height=0.4, label='2025', color='orange')

plt.yticks(y, ['Apples', 'Bananas', 'Cherries', 'Dates'])
plt.xlabel("Quantity")
plt.title("Grouped Horizontal Bar Chart")
plt.legend()
plt.show()

matplotlib bar chart horizontal grouped example




Stacked horizontal bar chart

import matplotlib.pyplot as plt
import numpy as np

categories = ['Apples', 'Bananas', 'Cherries', 'Dates']
y = np.arange(4)
values_2024 = [25, 40, 15, 30]
values_2025 = [30, 35, 20, 25]

plt.barh(categories, values_2024, label='2024', color='green')
plt.barh(categories, values_2025, left=values_2024, label='2025', color='lightgreen')

plt.title("Stacked Horizontal Bar Chart")
plt.xlabel("Quantity")
plt.legend()
plt.show()

matplotlib bar chart horizontal stacked example
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact