Home > Mobile >  How to create 2 plots side by side when there are two categories in your dataframe?
How to create 2 plots side by side when there are two categories in your dataframe?

Time:11-23

I have a dataset similar to this:

d = {'label': ['true', 'false', 'true', 'false', 'false'],
     'count': [4, 24, 31, 2, 3],
     'date': [2019-01-01, 2019-01-02, 2019-01-03, 2019-01-04, 2019-01-05]}
df = pd.DataFrame(d)

I am trying to create a cumulative sum using count for each label and plot them side by side. However, I've only been able to do so in the following way:

pivot = pd.pivot_table(df, values="count", index=["date"], columns=["label"], aggfunc=np.sum)
pivot = pivot.cumsum()

plt.rcParams["figure.figsize"] = [20,7]
plt.plot(pivot)
plt.ylabel('Cumulative number')
plt.xlabel('Time')

It works fine, except both categories are in a single plot. I'd like to create them side by side with y-label as something like 10^0, 10^1, 10^2 and so on, something like this:

enter image description here

Any guidance please?

CodePudding user response:

You can use seaborn:

import seaborn as sns

sns.relplot(data=pivot.melt(ignore_index=False), x='date', y='value', col='label', kind='scatter')

enter image description here

  • Related