Manu Sharma
Manu Sharma

Reputation: 1739

Stacked area chart for all the groups

I am trying to create a stacked area chart for all the groups in my data on a similar timeline x-axis. My data looks like following

dataDate  name prediction       
2018-09-30  A   2.309968
2018-10-01  A   1.516652
2018-10-02  A   2.086062
2018-10-03  A   1.827490
2018-09-30  B   0.965861
2018-10-01  B   6.521989
2018-10-02  B   9.219777
2018-10-03  B   17.434451
2018-09-30  C   6.890485
2018-10-01  C   6.106187
2018-10-02  C   5.535563
2018-10-03  C   1.913100

And I am trying to create something like following enter image description here

The x-axes will be the time series. Please help me to recreate the same. Thanks

Upvotes: 0

Views: 2061

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339795

Say your data is stored in a dataframe named df. Then you can pivot the dataframe and plot it directly. Make sure your dates are actual dates, not strings.

df["dataDate"] = pd.to_datetime(df["dataDate"])
df.pivot("dataDate", "name", "prediction").plot.area();

enter image description here

Upvotes: 4

Oli
Oli

Reputation: 1393

You can copy your data in clipboard and try something like this

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_clipboard()
fig, ax = plt.subplots()
for label, sub_df in df.set_index('dataDate').groupby('name'):
    sub_df.plot.area(ax=ax, label=label)
plt.legend()

Upvotes: 0

Related Questions