
In this post, you will learn how to create horizontal bar chart in plotly express and plotly graph objects.
1 . Create Horizontal Bar Chart using Plotly Express –
Load a dataset for plotting.
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv"
df = pd.read_csv(url)
df.head()

To create a horizontal bar chart in plotly express, we use the px.bar() with orientation=’h’ parameter.
fig = px.bar(df, x="Net Sales", y="Method of Payment", orientation='h')
fig.show()

To create a stacked horizontal bar chart use the color parameter.
fig = px.bar(df, x="Net Sales", y="Method of Payment", color='Gender', orientation='h')
fig.show()

To create a grouped horizontal bar chart use the barmode parameter. By default the color parameter makes a stacked horizontal bar chart.
fig = px.bar(
df,
x="Net Sales",
y="Method of Payment",
color="Gender",
barmode="group",
orientation="h",
)
fig.show()

If you look at the above plot you can see that px.bar() creates one rectangle for each row of data which results in strip look. If you want to combine these rectangles into one per color per position, then use the px.histogram(). The call signature of px.histogram is same as px.bar so you can easily switch between the two.
fig = px.histogram(
df,
x="Net Sales",
y="Method of Payment",
color="Gender",
barmode="group",
orientation="h",
)
fig.show()

2 . Create Horizontal Bar Chart with Plotly Graph Objects
To create a horizontal bar Chart with plotly graph objects, we use the go.Bar() with orientation=’h’ parameter.
fig = go.Figure()
fig.add_trace(go.Bar(x=df["Net Sales"], y=df["Method of Payment"], orientation="h"))
fig.update_layout(xaxis_title="Net Sales", yaxis_title="Method of Payment")
fig.show()

Grouped Horizontal Bar Chart –
To create a grouped bar chart, you have to first isolate the data for the groups and then create bar chart for each group and then set the barmode=’group’ in figure_update.
# select data for male and female
male = df[df["Gender"] == "Male"]
female = df[df["Gender"] == "Female"]
# plot grouped bar chart
fig = go.Figure()
fig.add_trace(
go.Bar(
x=male["Net Sales"], y=male["Method of Payment"], orientation="h", name="Male"
)
)
fig.add_trace(
go.Bar(
x=female["Net Sales"],
y=female["Method of Payment"],
orientation="h",
name="Female",
)
)
fig.update_layout(
barmode="group", xaxis_title="Net Sales", yaxis_title="Method of Payment"
)
fig.show()

Stacked Horizontal Bar Chart –
To create a stacked Bar chart just set barmode=’stack’
# select data for male and female
male = df[df["Gender"] == "Male"]
female = df[df["Gender"] == "Female"]
# plot grouped bar chart
fig = go.Figure()
fig.add_trace(
go.Bar(
x=male["Net Sales"], y=male["Method of Payment"], orientation="h", name="Male"
)
)
fig.add_trace(
go.Bar(
x=female["Net Sales"],
y=female["Method of Payment"],
orientation="h",
name="Female",
)
)
fig.update_layout(
barmode="stack", xaxis_title="Net Sales", yaxis_title="Method of Payment"
)
fig.show()

Related Posts –
1 . How to install plotly python with pip
2 . How to create a Line Chart with Plotly Python
3 . How to create Scatter plot in Plotly Python
4 . How to create a Bar Chart in Plotly Python
5 . How to create a Histogram in plotly python
6 . How to Create a Box Plot in Plotly Python
7 . How to create a Pie Chart in Plotly Python
8 . How to create a Dot Plot in Plotly Python
9 . How to Create Heatmap with Plotly Python
10 . How to Create a Violin Plot in Plotly Python
11 . How to Create Subplots in Plotly Python