9. Making Stacked Bars#

A stacked bar chart is a variation of the bar chart where each bar is divided into segments that represent different categories or parts of a whole. Instead of bars being placed side by side, the components are stacked on top of one another, allowing each bar to represent a total value with its internal segments showing how different parts contribute to that total. Stacked bar charts are useful when you want to compare both the total values and the breakdown of those values across categories, making it easier to visualize how the components of each group relate to one another and to the whole.

🚀 When to use them:

Stacked bar charts are particularly useful when you want to display how different sub-groups contribute to a total, especially when comparing across multiple categories or time periods. For instance, they are effective in cases like showing sales by product categories in different regions or the distribution of a budget across various departments over time.

⚠️ Be aware:

However, they have limitations when too many segments are included. When the bars are stacked with numerous categories, it becomes harder to compare the individual segments, especially for the middle or smaller sections. This can make it difficult to accurately assess differences between categories. Additionally, stacked bar charts may not be ideal if the focus is on comparing individual segment sizes rather than the overall total, as the stacking can obscure direct comparisons between parts.

Getting ready#

For this recipe, we need to read data from a csv file and then create a data set

import pandas as pd
data = pd.read_csv('data/data_recipe07.csv')
data = data[data.Entity != 'Total']
data.head()
Entity Code Year Global corporate investment in AI
0 Merger/acquisition NaN 2013 6885993015
1 Merger/acquisition NaN 2014 7657417878
2 Merger/acquisition NaN 2015 10117764887
3 Merger/acquisition NaN 2016 14733541676
4 Merger/acquisition NaN 2017 27282738436

How to do it#

  1. Import the plotly.express module as px

import plotly.express as px
df = data
  1. Make a minimal stacked bar chart by using the function bar and passing the inputs

  • x

  • y

  • color

fig = px.bar(df, x='Year', y='Global corporate investment in AI',
             color= 'Entity')
fig.show()
  1. Add a title to your chart by passing a string as the input title into the function bar; and customise the size of the figure by using the inputs height and width. Both have to be integers and correspond to the size of the figure in pixels.

fig = px.bar(df, x='Year', y='Global corporate investment in AI',
             color= 'Entity', 
             height = 500, width = 800,
             title = 'Global Corporate Investment in AI')
fig.show()
  1. Customise the colors used in the scatter by using the input color_discrete_sequence

fig = px.bar(df, x='Year', y='Global corporate investment in AI',
             color= 'Entity', 
             color_discrete_sequence= px.colors.qualitative.Prism,
             height = 500, width = 800,
             title = 'Global Corporate Investment in AI')
fig.show()
fig = px.bar(df, x='Year', y='Global corporate investment in AI',
             color= 'Entity', 
             color_discrete_sequence=px.colors.sequential.Plasma,
             height = 500, width = 800,
             title = 'Global Corporate Investment in AI')
fig.show()