在Seaborn情节中作为X的星期几

时间:2018-03-02 23:55:20

标签: python-3.x pandas matplotlib seaborn

我有一个包含点击次数和展示次数的数据集,我使用groupby和agg将它们汇总到星期几

df2=df.groupby('day_of_week',as_index=False, sort=True, group_keys=True).agg({'Clicks':'sum','Impressions':'sum'})

然后我试图用子图

绘制它们
f, axes = plt.subplots(2, 2, figsize=(7, 7))
sns.countplot(data=df2['Clicks'], x=df2['day_of_week'],ax=axes[0, 0])
sns.countplot(data=df2["Impressions"], x=df2['day_of_week'],ax=axes[0, 1])

但是使用星期几作为X,情节使用了点击次数和展示次数中的值。有没有办法强制X到星期几,而值是在Y?感谢。

完整代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style("whitegrid")

df=pd.read_csv('data/data_clean.csv')

df2=df.groupby('day_of_week',as_index=False, sort=True, group_keys=True).agg({'Clicks':'sum','Impressions':'sum'})

f, axes = plt.subplots(2, 2, figsize=(7, 7))
sns.countplot(data=df2['Clicks'], x=df2['day_of_week'],ax=axes[0, 0])
sns.countplot(data=df2["Impressions"], x=df2['day_of_week'],ax=axes[0, 1])

plt.show()

假数据:

day_of_week,Clicks,Impressions
 0           100       2000
 1           400       4000
 2           300       3500
 3           200       2000
 4           100       1000
 5           50        500
 6           10        150

1 个答案:

答案 0 :(得分:1)

根据seaborn文档,我认为countplot需要一个长形式的数据框,例如您的df而不是预先汇总的df2建立并传递你的问题。 countplot为你计算。

但是,您的df2已准备好进行大熊猫情节:

df2.plot(kind='bar', y=['Impressions', 'Clicks'])

结果: pandas bar plot of df2