I am trying to create a stacked area chart, which shows the number of customers by country.
So my data frame is:
date people country
2021-11-18 509 USA
2021-11-18 289 France
2021-11-18 234 Germany
2021-11-18 148 Poland
2021-11-18 101 China
I don't understand how to edit the graphic design (color).
table.groupby(['date','country'])['people'].sum().unstack().plot(
kind='area',
figsize=(10,4))
Also I tried to use the Bokeh library for nice visualization, but i don't know how to write the code
Thanks for your help. It's my first post. Sorry if I missed something.
I think your are looking for varea_stack()
-function in bokeh
.
My solution is based on the varea_stack
-example which is part of the official documentation.
Let's assume this is your data (I added on day):
text = """date people country
2021-11-18 509 USA
2021-11-18 289 France
2021-11-18 234 Germany
2021-11-18 148 Poland
2021-11-18 101 China
2021-11-19 409 USA
2021-11-19 389 France
2021-11-19 134 Germany
2021-11-19 158 Poland
2021-11-19 191 China"""
First I bring the data in the same form of the example:
from io import StringIO
import pandas as pd
df = pd.read_csv(StringIO(text), sep='\s+', parse_dates=True, index_col=0)
df = df.groupby(['date','country']).sum().unstack()
df.columns = df.columns.droplevel(0)
df.index.name=None
df.columns.name=None
Now the DataFrame looks like this:
China France Germany Poland USA
2021-11-18 101 289 234 148 509
2021-11-19 191 389 134 158 409
Now the rest is straight forward. If your index is a DatetimeIndex
you have to modify the x_axis_type
of the bokeh figure. Id did this for the plot below.
from bokeh.palettes import brewer
from bokeh.plotting import figure, show, output_notebook
output_notebook()
n = df.shape[1]
p = figure(x_axis_type='datetime')
p.varea_stack(stackers=df.columns, x='index', source=df, color=brewer['Spectral'][n],)
show(p)
The output lookslike this:
You can redefine the color using the color
-keyword if you like.