← 返回首页
Interactive stacked barplot with plotly

Interactive stacked barplot with plotly

This post shows how to plot a stacked barplot using the plotly library in python. In the previous post, we saw how to create a simple interactive barplot.

In this post, we will learn how to represent barplot with both categories and sub categories. The first example uses the plotly.express module and the second example uses the plotly.graph_objects module.

Dataset

For a barplot, we need a dataset with 3 columns: one for the main categories, one for the sub categories and one for the values.

Let's create it using pandas:

import pandas as pd data = { 'category': ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E'], 'sub_category': ['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y'], 'value': [17, 10, 42, 22, 33, 11, 55, 25, 25, 15] } df = pd.DataFrame(data) df.head()
category sub_category value 0 1 2 3 4
A X 17
A Y 10
B X 42
B Y 22
C X 33

Stacked Barplot with plotly express

The chart is made using using the bar() function from the plotly.express module.

We just have to pass the df object, and specify which column we want to plot. In this case it's the category and the value (defined above).

Then we just need to specify the color argument with the sub_category column so that colors map this column.

import plotly.express as px fig = px.bar( df, x='category', y='value', color='sub_category' ) fig.show()

Now we save it as an html element in order to render it

# save widget to html fig.write_html( "../../static/interactiveCharts/583-stacked-barplot-plotly-1.html" )
%%html <iframe src="../../interactiveCharts/583-stacked-barplot-plotly-1.html" width="790" height="700" title="Stacked Barplot with plotly express" style="border:none"> </iframe>

Barplot with plotly graph object

The plotly graph object API requires a bit more verbose, but nothing very complex here!

We mainly have to iterate over each sub categories, filter the dataframe on this sub category, and plot it.

Last but not least, we use the update_layout() function with the barmode='stack' argument to ensure that bars are stacked on each others. Otherwise the default render will put them next to each other (the default value is group)

import plotly.graph_objects as go fig = go.Figure() # Create list of all unique sub categories sub_categories = df['sub_category'].unique() # Adding bars for each sub-category for sub_cat in sub_categories: filtered_df = df[df['sub_category'] == sub_cat] fig.add_trace(go.Bar( x=filtered_df['category'], y=filtered_df['value'], name=sub_cat )) # Update layout if necessary fig.update_layout( barmode='stack' ) # Show plot fig.show()

Now we save it as an html element in order to render it

# save widget to html fig.write_html( "../../static/interactiveCharts/583-stacked-barplot-plotly-2.html" )
%%html <iframe src="../../interactiveCharts/583-stacked-barplot-plotly-2.html" width="790" height="790" title="Stacked Barplot with plotly graph object" style="border:none"> </iframe>

Going further

You might be interested in:

Barplot

Spider / Radar

Wordcloud

Parallel

Lollipop

Circular Barplot

Table

🚨 Grab the Data To Viz poster!


Do you know all the chart types? Do you know which one you should pick? I made a decision tree that answers those questions. You can download it for free!

    Get Poster