How can I create a horizontal barplot in Seaborn? 2

How can I create a horizontal barplot in Seaborn?

Seaborn is a popular Python library used for data visualization. One of its key features is the ability to create horizontal barplots, which are useful for comparing the values of different categories. To create a horizontal barplot in Seaborn, you can use the “barplot” function and specify the x and y variables, as well as the data source. Additionally, you can customize the appearance of the plot by adjusting parameters for the color, width, and labels. This allows for the creation of a clear and informative visual representation of the data. Overall, with Seaborn’s user-friendly syntax and customizable options, creating a horizontal barplot is a simple and effective way to present your data.

Create a Horizontal Barplot in Seaborn (With Example)


You can use the following basic syntax to create a horizontal barplot in :

sns.barplot(x=df.values_var, y=df.group_var, orient='h')

The orient=’h’ argument tells seaborn to orient the bars horizontally instead of the default vertical.

The following example shows how to use this syntax in practice.

Example: How to Create a Horizontal Barplot in Seaborn

Suppose we have the following pandas DataFrame that contains information about the total sales made by various employees at a company:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'employee': ['Andy', 'Bert', 'Chad', 'Doug', 'Eric', 'Frank'],
                   'sales': [22, 14, 9, 7, 29, 20]})

#view DataFrame
print(df)

  employee  sales
0     Andy     22
1     Bert     14
2     Chad      9
3     Doug      7
4     Eric     29
5    Frank     20

We can use the following syntax to create a horizontal barplot to visualize the sales by each employee:

import seaborn as sns

#create horizontal barplot
sns.barplot(x=df.sales, y=df.employee, orient='h')

The x-axis displays the sales made by each employee and the y-axis shows the names of the employees.

Note that we can also specify the colors of the bars and add a custom title with axis labels:

import matplotlib.pyplotas plt
import seaborn as sns

#create horizontal bar chart
sns.barplot(x=df.sales, y=df.employee, color='steelblue', orient='h')

#add plot title
plt.title('Total Sales by Employee', fontsize=16)

#add axis labels
plt.xlabel('Total Sales')
plt.ylabel('Employee Name')

seaborn horizontal barplot

The bars in the plot now each have the same color and we’ve added an overall plot title and axis labels to make the plot easier to read.

Note: If you have trouble importing seaborn in a Jupyter notebook, you may first need to run the command %pip install seaborn.

How to Create a Grouped Barplot in Seaborn

Cite this article

stats writer (2024). How can I create a horizontal barplot in Seaborn?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-horizontal-barplot-in-seaborn/

stats writer. "How can I create a horizontal barplot in Seaborn?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-horizontal-barplot-in-seaborn/.

stats writer. "How can I create a horizontal barplot in Seaborn?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-horizontal-barplot-in-seaborn/.

stats writer (2024) 'How can I create a horizontal barplot in Seaborn?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-horizontal-barplot-in-seaborn/.

[1] stats writer, "How can I create a horizontal barplot in Seaborn?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a horizontal barplot in Seaborn?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top