How to create a Bar Chart in Plotly Python.

Spread the love

In this post, you will learn how to create a bar chart with plotly express and plotly graph objects.

1 . Create a Bar Chart with Plotly Express –

Read the data to work with.

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 bar chart in plotly express, we can use the px.bar() function.

fig = px.bar(df, x="Method of Payment", y="Net Sales")
fig.show()

Stacked Bar chart –

To create a stacked bar chart use the color parameter.

fig = px.bar(df, x="Method of Payment", y="Net Sales", color="Gender")
fig.show()

Grouped Bar Chart –

The default behavior of color parameter is to create a stacked bar chart. If you want to create a grouped bar chart then use the barmode argument.

fig = px.bar(df, x="Method of Payment", y="Net Sales", color="Gender", barmode="group")
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="Method of Payment", y="Net Sales", color="Gender", barmode="group"
)
fig.show()

By default histogram sums the y values but if you want it to be average then you can do that with the histfunc parameter.

fig = px.histogram(
    df,
    x="Method of Payment",
    y="Net Sales",
    color="Gender",
    barmode="group",
    histfunc="avg",
)
fig.show()

You can also add text to your bar chart by setting the text_auto parameter to True.

fig = px.histogram(
    df, x="Method of Payment", y="Net Sales", color="Gender", text_auto=True
)
fig.show()

By default, plotly will adjust the text labels for you. But if you want to change them, you can do that with the textfont, textposition and textangle.

fig = px.histogram(
    df, x="Method of Payment", y="Net Sales", color="Gender", text_auto=True
)
fig.update_traces(textfont_size=12, textangle=0, textposition="outside")
fig.show()

You can also create subplots or facet plots using the facet_col and facet_row parameter.

fig = px.histogram(
    df,
    x="Method of Payment",
    y="Net Sales",
    color="Gender",
    facet_col="Marital Status",
)
fig.show()

2 . Create a Bar Chart with Plotly Graph Objects –

fig = go.Figure()
fig.add_trace(go.Bar(x=df["Method of Payment"], y=df["Net Sales"]))
fig.update_layout(xaxis_title="Method of Payment", yaxis_title="Net Sales")
fig.show()

Grouped 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["Method of Payment"], y=male["Net Sales"], name="Male"))
fig.add_trace(
    go.Bar(x=female["Method of Payment"], y=female["Net Sales"], name="Female")
)
fig.update_layout(
    barmode="group", xaxis_title="Method of Payment", yaxis_title="Net Sales"
)
fig.show()

Stacked 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["Method of Payment"], y=male["Net Sales"], name="Male"))
fig.add_trace(
    go.Bar(x=female["Method of Payment"], y=female["Net Sales"], name="Female")
)
fig.update_layout(
    barmode="stack", xaxis_title="Method of Payment", yaxis_title="Net Sales"
)
fig.show()

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 Horizontal 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

Rating: 1 out of 5.

Leave a Reply