简体   繁体   English

python:累计密度plot

[英]python: cumulative density plot

I have the following dataframe:我有以下 dataframe:

df = 
   Time_to_event  event
0         0 days    443
1         1 days    226
2         2 days    162
3         3 days     72
4         4 days     55
5         5 days     30
6         6 days     36
7         7 days     18
8         8 days     15
9         9 days     14
10       10 days     21
11       11 days     13
12       12 days     10
13       13 days     10
14       14 days      8

I want to produce a cumulative density plot of the sum of the events per days.我想生成每天事件总和的累积密度 plot。 For example 0 days 443, 1 days = 443 + 226 etc.例如 0 天 443、1 天 = 443 + 226 等。

I am currently trying this code:我目前正在尝试这段代码:

    stat = "count"  # or proportion
    sns.histplot(df, stat=stat, cumulative=True, alpha=.4)

but I come up with a pretty terrible plot:但我想出了一个非常糟糕的 plot: 在此处输入图像描述

If I could also come up with a line instead of bars that would be awesome!如果我也能想出一条线而不是酒吧,那就太棒了!

You can try a combo of pandas.Series.cumsum and seaborn.lineplot :您可以尝试pandas.Series.cumsumseaborn.lineplot组合

df["cumsum"] = df["event"].cumsum()

plt.figure(figsize=(6,4))
sns.lineplot(x="Time_to_event", y="cumsum", data=df);

Output: Output:

在此处输入图像描述

I think what you are looking for your plot values is:我认为您要查找的 plot 值是:

xvalues=df["Time_to_event"]
yvalues=df["event"].cumsum()

The code could look like this:代码可能如下所示:

import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("test.txt")
print(df.columns)
print(df)

plt.bar(df["Time_to_event"],df["event"].cumsum()) 
# replace plt.bar with plt.plot for a plotted diagram
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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