日期时间堆积条形图与来自数据帧的 Matplotlib [英] Datetime Stacked Bar Chart with Matplotlib from dataframe

查看:50
本文介绍了日期时间堆积条形图与来自数据帧的 Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我被困住了.或者只是有一个金发碧眼的时刻.我就像一只看着车灯的鹿.

我从MySQL表中查询了以下数据框.

日期操作2020-05-07 A2020-05-08 B2020-05-08 A2020-05-12 A2020-05-12 A2020-05-12 乙2020-05-13 C2020-05-13 A2020-05-13 乙2020-05-14 A2020-05-19 B2020-05-21 A2020-05-25 A2020-05-26 B2020-05-26 C2020-05-26 A2020-05-26 A2020-05-29 A

我不知道如何使用 matplotlib 使其成为堆叠条形图.

研究:

  1. 如果需要每月,则可以使用 dt.to_period('M')获取每月日期和 crosstab :

    pd.crosstab(df.date.dt.to_period('M'),df.operation).plot.bar(stacked=True)

    输出(对于样本数据):

    <小时>

    更新以在 x 轴上显示所有日期:

      df ['date'] = pd.to_datetime(df.date)注意,maxdate = df ['date'].agg(['min','max'])all_dates = pd.date_range(mindate,maxdate,freq ='D').date(pd.crosstab(df.date,df.operation).reindex(all_dates).plot.bar(stacked=True))

    输出:

    Background:

    I am stuck. Or just having a blonde moment. I'm like a deer looking at headlights.

    I've queried the following dataframe from MySQL table.

    date          operation
    2020-05-07        A
    2020-05-08        B
    2020-05-08        A
    2020-05-12        A
    2020-05-12        A
    2020-05-12        B
    2020-05-13        C
    2020-05-13        A
    2020-05-13        B
    2020-05-14        A
    2020-05-19        B
    2020-05-21        A
    2020-05-25        A
    2020-05-26        B
    2020-05-26        C
    2020-05-26        A
    2020-05-26        A
    2020-05-29        A
    

    I have no idea how to make it a stacked bar chart with matplotlib.

    Research:

    1. Grouped Bar-Chart with customized DateTime Index using pandas and Matplotlib
    2. Stacked bar plot using Matplotlib

    Question:

    How can I generate a stack-bar-chart with matplot lib with the above sample data?

    Code snippet:

    import datetime as dt
    import mysql.connector
    import os
    import pandas as pd
    # import numpy as np
    import matplotlib.pyplot as plt
    import datetime
    
    def generate_monthly_graph():
        query = "SELECT [...]`"
        mycursor.execute(query)
        mycursor.execute(query)
        data = mycursor.fetchall()
    
        df = pd.DataFrame(data, columns=['date', 'operation'])
    
        df = df.set_index('date')
    
        df.index = pd.to_datetime(df.index)
    

    解决方案

    You can use crosstab then plot.bar:

    # convert to date, no need for set_index
    df.date = pd.to_datetime(df.date)
    
    pd.crosstab(df.date,df.operation).plot.bar(stacked=True)
    

    Output:

    If you want monthly, you can use dt.to_period('M') to get monthly dates and crosstab:

    pd.crosstab(df.date.dt.to_period('M'),df.operation).plot.bar(stacked=True)
    

    Output (for the sample data):


    Update to have all the dates on the x-axis:

    df['date'] = pd.to_datetime(df.date)
    
    mindate, maxdate = df['date'].agg(['min','max'])
    
    all_dates = pd.date_range(mindate, maxdate, freq='D').date
    
    (pd.crosstab(df.date,df.operation)
       .reindex(all_dates)
       .plot.bar(stacked=True)
    )
    

    Output:

    这篇关于日期时间堆积条形图与来自数据帧的 Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆