Search code examples
pythonpandasmatplotlibstacked-area-chart

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


Solution

  • 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