每个百分点的python绘图线

问题描述 投票:0回答:1

如何使用seaborn为每个百分位数绘制通过pandas.DataFrame.describe计算的百分位数?

目前,我需要遍历每一个。相反,我想要一个包含所有百分位数的图表。https://seaborn.pydata.org/generated/seaborn.lineplot.html有一些带有色相和样式的漂亮示例,但是我目前想知道如何正确地重塑数据框以能够使用此方法。

import pandas as pd
%pylab inline

df = pd.DataFrame({'dt':['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-03'], 'bar':[1,2,3, 4], 'baz':[3,4,5, 6]})
df['dt'] = pd.to_datetime(df['dt'])
display(df)

df = df.groupby(['dt']).describe()
df = df.reset_index()
df = df.set_index(['dt'], drop=False)
display(df)

import seaborn as sns; sns.set()

# iterate for each column (bar, baz)
df_plot = df[['dt']].copy()

# iterate for each percentile
df_plot['metric'] = df['bar']['25%']
sns.lineplot(x='dt', y='metric', data=df_plot)
plt.show()

df_plot['metric'] = df['bar']['50%']
sns.lineplot(x='dt', y='metric', data=df_plot)
plt.show()

df_plot['metric'] = df['bar']['75%']
sns.lineplot(x='dt', y='metric', data=df_plot)
plt.show()
python pandas seaborn reshape percentile
1个回答
1
投票

您可以使用以下方法简化所有步骤:

import pandas as pd
%pylab inline

df = pd.DataFrame({'dt':['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-03'], 'bar':[1,2,3, 4], 'baz':[3,4,5, 6]})
df = df.groupby(['dt']).describe()
sns.lineplot(data=df['baz'][['25%', '50%', '75%']])

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.