Search code examples
pythonpandasmatplotlibseabornbar-chart

time series bar plot showing the values being the sum for a given time period


There has a time series data, such as the following ones.

import pandas as pd
data = {'Time': ['2/10/2019', '3/3/2019', '3/15/2019', '3/25/2019', '4/16/2019', '4/17/2019', '5/6/2019', '5/18/2019'],
        'Order nun': [200, 150, 50, 100, 90, 190, 120, 110]}
df = pd.DataFrame(data)
        Time  Order nun
0  2/10/2019        200
1   3/3/2019        150
2  3/15/2019         50
3  3/25/2019        100
4  4/16/2019         90
5  4/17/2019        190
6   5/6/2019        120
7  5/18/2019        110

How to generate a time series bar plot based on the sum of monthly value.

enter image description here


Solution

  • You can set the Time as index and use pd.Grouper(freq='M') to groupby month

    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    df['Time'] = pd.to_datetime(df['Time'])
    out = df.set_index('Time').groupby(pd.Grouper(freq='M'))['Order number'].sum()
    
    fig, ax = plt.subplots()
    bars = ax.bar(out.index, out)
    
    ax.bar_label(bars)
    
    ax.set_xlabel("Time (month)")
    ax.set_ylabel("Order number")
    
    ax.set_xticks(out.index)
    ax.set_yticks(range(200, 800, 200))
    ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))
    
    plt.show()
    

    enter image description here

    The reason why the bar is so thin is that the bar only takes one day in a month. You can use string instead to make it normal.

    df['Time'] = pd.to_datetime(df['Time']).dt.strftime('%b %Y')
    out = df.groupby('Time')['Order number'].sum()
    
    fig, ax = plt.subplots()
    bars = ax.bar(out.index, out)
    
    ax.bar_label(bars)
    
    ax.set_xlabel("Time (month)")
    ax.set_ylabel("Order number")
    
    ax.set_xticks(out.index)
    ax.set_yticks(range(200, 800, 200))
    
    plt.show()
    

    enter image description here