0

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Manu Sharma
  • 1,593
  • 4
  • 25
  • 48

2 Answers2

4

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

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()
Oli
  • 1,313
  • 14
  • 31