0
votes

I struggle with this: I try to represent the same data monthly with a bar chart and the total with a pie chart but I can't get the same color representing the same category on both graphs. My code looks like:

import pandas as pd
import plotly.express as px
from plotly.offline import plot

df = pd.read_csv('myfile.csv')
data = df.groupby(['month', 'category']).size().reset_index(name='number of records')
histo = px.bar(data, x='month', y='number of records', color='category', barmode='stack')
pie = px.pie(data, values='number of records', names='category')
plot(histo, filename = 'histo')
plot(pie, filename = 'pie')

And the result is:

enter image description here enter image description here

As we can see A is blue on bar chart while X is blue on pie chart.

Can anyone helps?

Tks

1

1 Answers

0
votes

OK, it was quite obvious finally: just add color key in pie chart like this:

pie = px.pie(data, values='number of records', names='category', color='category')