绘制随时间变化的频率分布(叠加)

2024-07-08 08:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图根据日期来计算一家商店里有多少人。 为此,我首先按日期对人员姓名进行分组:

df.groupby(['Date', 'Name']).size()

结果如下所示

    Date        Name  


2020-01-25  John       1
2020-01-26  John       1
2020-01-27  John       1
2020-01-28  Luca       1
            John       1
                 ..
2020-03-30  John       1
2020-03-31  Martyn     2
            Christine  1
            Mary       1
            John       1

现在,我想看看每天有多少人(1{},1{},1{},3{}等等)以及谁。 所以我需要一个直方图(条形图),可以帮助我可视化这些结果。为了在同一天看到同一个人的频率(比如Martyn,两次),我需要考虑一个堆叠的情节(我想)。 因为名字的数量大约是1000个,你能告诉我是否有一个可以阅读的图例/标签吗

你能告诉我怎么做吗?情节是我的弱点(不幸的是)


Tags: namedfsizedate人员john商店姓名
1条回答
网友
1楼 · 发布于 2024-07-08 08:21:27

我喜欢使用seaborn和maplotlib的组合。Seaborn有非常简单的开箱即用的图形供您使用,然后您可以使用matplotlib自定义它们

import seaborn as sns
import matplotlib.pyplot as plt

#create blank figure instance using matplotlib syntax, for easier customisation
fig,ax1 = plt.subplots(figsize=(8,5))

#manipulate your data
dfg = df.groupby(['Date', 'Name']).size().reset_index()

#call the 'barplot' from seaborn, which is what you're after. You could even use 
#'countplot' and skip the groupby step in your data.
sns.barplot(data=dfg,x='Date',y='Size',ax=ax1)

#customise your plot with matplotlib
ax1.set_title('Customers attending shops')

相关问题 更多 >

    热门问题