Creating Horizontal Bar Charts using Pandas
Last Updated :
02 Dec, 2020
Prerequisites: Pandas
A bar chart represents categorical data with corresponding data values as rectangular bars. Usually, the x-axis represents categorical values and the y-axis represents the data values or frequencies. This is called a vertical bar chart and the inverse is called a horizontal bar chart. In some cases, a horizontal bar chart provides better readability.
Python has various visualization libraries such as Matplotlib and Seaborn. The Pandas library, having a close integration with Matplotlib, allows creation of plots directly though DataFrame and Series object. This article explores the methods to create horizontal bar charts using Pandas.
Using the plot instance of the Pandas DataFrame, various kinds of graphs can be created including Bar charts. There are two types of bar charts that represent complex categories:
- Grouped or Compounded Bar Charts - When you have sub-categories of a main category, this graph assigns each variable or sub-category with a separate bar in the corresponding category.
- Stacked Bar Charts - When you have sub-categories of a main category, this graph stacks the sub-categories on top of each other to produce a single bar.
The bar() and barh() methods of Pandas draw vertical and horizontal bar charts respectively. Essentially, DataFrame.plot(kind="bar") is equivalent to DataFrame.plot.bar(). Below are some examples to create different types of bar charts using the above mentioned functions.
Simple horizontal bar chart
Syntax:
DataFrame.plot.barh()
The barh() methods accept x and y parameters where x takes the categorical values (by default, it takes the index of the DataFrame) and y takes all the numeric columns. The keyword arguments (like title or figure size) supported by DataFrame.plot() can be passed to the barh() method in order to customize the bar chart. Given is the implementation depicting a horizontal bar chart representing the number of people that preferred particular categories of cuisine.
Example:
Python3
# Import required libraries
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'Cuisine': ['Italian', 'Indian', 'Mexican', 'Chinese'],
'Number of People': [20, 25, 15, 10]})
# Plot a bar chart
df.plot.barh(x='Cuisine', y='Number of People',
title='Cuisine Preference', color='green')
Output:

Compounded horizontal bar chart
From the above example, if people are divided into sub-groups of males and females then we can represent this data with a compounded horizontal bar chart. This example shows a horizontal bar chart representing the number of males and females that preferred particular categories of cuisine using two methods.
Example 2:
Bar chart using barh() method
Python3
# Import required libraries
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'Number of Males': [10, 15, 25, 14],
'Number of Females': [20, 25, 15, 10]},
index=['Italian', 'Indian', 'Mexican', 'Chinese'])
# Plot grouped horizontal bar chart
df.plot.barh(title="Gender wise Cuisine preference chart",
color={"green", "pink"})
Output:
Example 3:
Bar chart using plot() method
Python3
# Import required libraries
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'Number of Males': [10, 15, 25, 14],
'Number of Females': [20, 25, 15, 10]},
index=['Italian', 'Indian', 'Mexican', 'Chinese'])
# Plot stacked horizontal bar chart
df.plot(kind="barh", title="Gender wise Cuisine preference chart",
color={"green", "pink"})
Output:

Stacked horizontal bar chart
Stacked bar charts are useful in representing the composition or contribution of different sub-groups. The example below shows a horizontal bar chart representing the percentage of males and females that preferred particular categories of cuisine. A stacked horizontal bar chart places the values at each observation in the dataframe side by side in a single bar. However, it stacks the numeric values rather than the percentage of a whole. So, we first convert the data values into the percentage of a whole then use the barh() function with the stacked parameter set to True to create a filled stacked horizontal bar chart.
Example 4:
Python3
# Import required libraries
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'Number of Males': [10, 15, 25, 14],
'Number of Females': [20, 25, 15, 10]},
index=['Italian', 'Indian', 'Mexican', 'Chinese'])
# Convert numeric values to percentage of whole
percent_df = df.apply(lambda x: (x * 100) / sum(x), axis=1)
# Plot stacked horizontal bar chart
percent_df.plot.barh(stacked=True,
title="Male-Female percentage composition of Cuisine preferences")
Output:
Similar Reads
Creating Charts using openpyxl
Charts are a powerful way to visualize data, making it easier to analyze and present information. While working with Excel files in Python, the library openpyxl comes into the picture and can be used as a tool for automating tasks like reading data, writing data, formatting cells, and of course crea
6 min read
Bar chart using Plotly in Python
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization librar
4 min read
Using pandas crosstab to create a bar plot
In this article, we will discuss how to create a bar plot by using pandas crosstab in Python. First Lets us know more about the crosstab, It is a simple cross-tabulation of two or more variables. What is cross-tabulation? It is a simple cross-tabulation that help us to understand the relationship be
3 min read
Diverging Bar Chart using Python
Diverging Bar Charts are used to ease the comparison of multiple groups. Its design allows us to compare numerical values in various groups. It also helps us to quickly visualize the favorable and unfavorable or positive and negative responses. The bar chart consisted of a combination of two horizon
3 min read
Create lollipop charts with Pandas and Matplotlib
In this article, we will create Lollipop Charts. They are nothing but a variation of the bar chart in which the thick bar is replaced with just a line and a dot-like âoâ (o-shaped) at the end. Lollipop Charts are preferred when there are lots of data to be represented that can form a cluster when re
4 min read
Draw a horizontal bar chart with Matplotlib
Matplotlib is the standard python library for creating visualizations in Python. Pyplot is a module of Matplotlib library which is used to plot graphs and charts and also make changes in them. In this article, we are going to see how to draw a horizontal bar chart with Matplotlib. Creating a vertica
2 min read
How to create a bar chart and save in pptx using Python?
World Wide Web holds large amounts of data available that is consistently growing both in quantity and to a fine form. Python API allows us to collect data/information of interest from the World Wide Web. API is a very useful tool for data scientists, web developers, and even any casual person who w
10 min read
Create Grouped Bar Chart using Altair in Python
Grouped bar charts are a handy tool to represent our data when we want to compare multiple sets of data items one against another. To make a grouped bar chart, we require at least three rows of three columns of data in our dataset. The three columns can be used as- one for values, one for series, an
3 min read
Filled area chart using plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histograms, bar plots, box plots, spread plots, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization
6 min read
How to create Stacked bar chart in Python-Plotly?
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read