【问题标题】:Plotting frequency distribution through time (stacked)绘制随时间变化的频率分布(堆叠)
【发布时间】:2020-06-01 09:27:43
【问题描述】:

我正在尝试按日期绘制商店中有多少人。 为此,我首先按日期人名分组:

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

现在,我想看看每天有多少人(125/01126/01127/01328/01、等等),以及谁。 所以我需要一个直方图(条形图)来帮助我可视化这些结果。为了查看同一天同一个人的频率(比如 Martyn,两次),我需要考虑堆叠图(我认为)。 由于名称的数量约为 1000,您能否告诉我是否有可能拥有可读的图例/标签?

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

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    我喜欢结合使用 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')
    

    【讨论】:

    • 嗨@KWx。我收到了这个警告:/anaconda3/lib/python3.7/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result. return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval
    • 我不确定抱歉。当您执行 groupby 时,它会创建一个多索引,这是一个元组序列。大概就是指那个吧。图表有效吗?
    • 是的,确实如此。谢谢KWx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多