Leonardo
Leonardo

Reputation: 11397

Pandas - Multiline plot

My data frame is composed by 3 elements: Name, Date and Weight.

I would like to plot a line graph where the X axis is the date, the Y is the COUNT OF weights (how many times a give subject weighted himself throughout the day) and each line is a different name.

In order to do that, i gave it a shot:

dtFrame.sort_values(['date'])

dtFrame.groupby(by=["date","name"])
dtFrame.plot.line()
plt.show()

but that was not what I expected... basically what i want is: Select name, date, count(1) FROM myTable Groupby name, date

What am i doing wrong?

Edit1: Sample csv data

day, name, weight
1,a,5
1,a,5
1,a,7
1,b,5
1,b,5
2,a,5
2,a,5
2,a,7
2,b,5
2,b,5
3,a,5
3,a,5
3,a,7
3,b,5
3,b,5
4,a,5
4,a,5
4,a,7
4,b,5
4,b,5

Upvotes: 1

Views: 122

Answers (3)

Stef
Stef

Reputation: 30679

res = df.groupby(['name','day']).count().unstack().T
res.index = res.index.droplevel()
res.plot()

enter image description here

Upvotes: 1

checkThisOut
checkThisOut

Reputation: 631

i would do it like that

df.groupby(['day', 'name']).weight.count().unstack().plot()

The resulting plot

Upvotes: 0

VersBersch
VersBersch

Reputation: 193

One way is to just loop through the names and plot each individually

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 18))

for name, group in data.groupby('name'):
    group.date.value_counts().plot(ax=ax, label=name)

plt.legend()
plt.show()

Upvotes: 2

Related Questions