我想按日期绘制商店里有多少人。为此,首先我按日期人的名字分组:
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
on 25/01
,1
on 26/01
,1
on 27/01
,3
on28/01
等等),以及谁。因此,我需要一个直方图(条形图)来帮助我可视化这些结果。为了查看同一天同一个人的出现频率(例如Martyn,该频率两次),我需要考虑一个堆积图(我认为)。由于名称的数量大约为1000,您能否告诉我是否有可能具有可读性的图例/标签?
您能告诉我该怎么做吗?情节是我的弱点(不幸的是)。
我喜欢结合使用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')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句