0

How do I plot multiple plots for each of the groups (each ID) below with Seaborn? I would like to plot two plots, one underneath the other, one line (ID) per plot.

ID    Date           Cum Value  Daily Value
3306  2019-06-01      100.0     100.0
3306  2019-07-01      200.0     100.0
3306  2019-08-01      350.0     150.0
4408  2019-06-01      200.0     200.0
4408  2019-07-01      375.0     175.0
4408  2019-08-01      400.0     025.0

This only plots both lines together and can look messy if there are 200 unique IDs.

sns.lineplot(x="Date", y="Daily Value",
             hue="ID", data=df)
3
  • Instead of a single plot with 200 lines, you want 2 plots with 100 lines each? Commented Jan 7, 2020 at 20:37
  • I just want two plots, one on top of the other for each unique ID, regardless if there are 2 or 200. One line for each plot. Thanks! Commented Jan 7, 2020 at 20:39
  • Then perhaps just make a separate figure per ID. I think it's going to be more trouble to try to get the sizing correct with 200 subplots: for idx, gp in df.groupby('ID'): fig, ax = plt.subplots(); gp.plot(x='Date', y='Daily Value', ax=ax, title=idx) Commented Jan 7, 2020 at 20:46

1 Answer 1

1

you can use

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'id': [3306, 3306, 3306, 4408, 4408, 4408],
                   'date': ['2019-06-01', '2019-07-01', '2019-08-01', '2019-06-01', '2019-07-01', '2019-08-01'],
                   'cum': [100, 200, 350, 200, 375, 400],
                   'daily': [100, 100, 150, 200, 175, 25]
})

g = sns.FacetGrid(df, col = 'id')
g.map(plt.plot, 'date', 'daily')

which gives

enter image description here

but what happens if you have 200 ids?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @SchwarzeHuhn, how do I stack these two charts instead of having them side by side?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.