Skip to content
Advertisement

Multiple color argument in plotly in python

>df.to_clipboard(sep=',', index=True)

,Country,Type,Year,Count
0,USA,A,1980,0.54
1,USA,A,1990,0.53
2,USA,A,2000,0.8
3,USA,A,2010,0.81
4,USA,A,2020,0.57
5,USA,B,1980,0.79
6,USA,B,1990,0.67
7,USA,B,2000,0.34
8,USA,B,2010,0.52
9,USA,B,2020,0.29
10,Canada,A,1980,0.91
11,Canada,A,1990,0.84
12,Canada,A,2000,0.85
13,Canada,A,2010,0.98
14,Canada,A,2020,0.81
15,Canada,B,1980,0.57
16,Canada,B,1990,0.61
17,Canada,B,2000,0.87
18,Canada,B,2010,0.16
19,Canada,B,2020,0.43

My objective is make a bar plot in plotly.

I did

import plotly.express as px
fig = px.bar(df, x="Year",y='Count',barmode='group',color="Country",hover_data=["Type"])
fig.show()

I get

enter image description here

I want Type A & Type B to have different colors, and preferable additional separate legend. I tried:

fig = px.bar(cnt, x="Year",y='Count',barmode='group',color=["Country","Type"],hover_data=["Type"])

but this gives error.

Advertisement

Answer

Using color to illustrate two different dimensions of your data would, in my opinion, quickly become very confusing. Unless you were able to use different shades of the same color. But even that would become messy with many categories. Luckily, plotly now provides another way to highlight multiple dimensions, namely Patterned Bar Charts with Plotly Express:

fig = px.bar(df, x="Year",y='Count',barmode='group',color="Country",hover_data=["Type"], pattern_shape = 'Type')

Plot 1

enter image description here

And you can change how the bars are grouped or stacked through barmode:

fig = px.bar(df, x="Year",y='Count',barmode='stack',color="Country",hover_data=["Type"], pattern_shape = 'Type')

Plot 2

enter image description here

Complete code

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

df = pd.DataFrame({'Country': {0: 'USA',
  1: 'USA',
  2: 'USA',
  3: 'USA',
  4: 'USA',
  5: 'USA',
  6: 'USA',
  7: 'USA',
  8: 'USA',
  9: 'USA',
  10: 'Canada',
  11: 'Canada',
  12: 'Canada',
  13: 'Canada',
  14: 'Canada',
  15: 'Canada',
  16: 'Canada',
  17: 'Canada',
  18: 'Canada',
  19: 'Canada'},
 'Type': {0: 'A',
  1: 'A',
  2: 'A',
  3: 'A',
  4: 'A',
  5: 'B',
  6: 'B',
  7: 'B',
  8: 'B',
  9: 'B',
  10: 'A',
  11: 'A',
  12: 'A',
  13: 'A',
  14: 'A',
  15: 'B',
  16: 'B',
  17: 'B',
  18: 'B',
  19: 'B'},
 'Year': {0: 1980,
  1: 1990,
  2: 2000,
  3: 2010,
  4: 2020,
  5: 1980,
  6: 1990,
  7: 2000,
  8: 2010,
  9: 2020,
  10: 1980,
  11: 1990,
  12: 2000,
  13: 2010,
  14: 2020,
  15: 1980,
  16: 1990,
  17: 2000,
  18: 2010,
  19: 2020},
 'Count': {0: 0.54,
  1: 0.53,
  2: 0.8,
  3: 0.81,
  4: 0.57,
  5: 0.79,
  6: 0.67,
  7: 0.34,
  8: 0.52,
  9: 0.29,
  10: 0.91,
  11: 0.84,
  12: 0.85,
  13: 0.98,
  14: 0.81,
  15: 0.57,
  16: 0.61,
  17: 0.87,
  18: 0.16,
  19: 0.43}})

fig = px.bar(df, x="Year",y='Count',barmode='group',color="Country",hover_data=["Type"], pattern_shape = 'Type')

fig.show()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement