简体   繁体   中英

how can pandas plot 2 pdf in same graph?

If only 1 line, i can plot, learn from below Plot Normal distribution with Matplotlib

eg , i have a pandas like below:

name,distance
Peter,13
Sam,14
Peter,15
Sam,12
Sam,13
Peter,14

With df.groupby('name').describe() I can display some min/max/mean by each of a user.

However, I want to draw a normal distribution base on existing data. i tried df.sort_values(by='name').groupby('name').plot()

but it wont draw a pdf or normal distribution for it. How can I use numpy to achieve that

Thanks

IIUC, what you want is to plot a distance histogram for both name values in the same plot.

import matplotlib.pyplot as plt
df = pd.DataFrame({'name':['Peter', 'Sam', 'Peter', 'Sam', 'Sam', 'Peter'],
         'distance':[13, 14, 15, 12, 13, 14]})

for name in df['name'].unique():      
  plt.hist(df.loc[df['name']==name, 'distance'], label=name)    
plt.legend();

在此输入图像描述


UPDATE:

As OP suggested in the comments, it's possible to draw these without a for loop.

df.groupby('name').distance.plot.hist()
df.groupby('name').distance.plot.kde()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM