根据星期时间统计日期总量,绘制matplotlib,pandas,Python

x33g5p2x  于2022-08-17 转载在 Python  
字(2.9k)|赞(0)|评价(0)|浏览(544)

根据星期时间统计日期总量,绘制matplotlib图,pandas,Python

每一个日期都有一个对应的星期几日期,现在假设有很多随机的日期,然后逐一获取日期对应的星期几数字,然后统计所有日期的星期几出现次数,最终划入到星期一到星期日7个分类,绘制统计图表:

import datetime
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from random import randrange
from datetime import timedelta

# 生成随机测试时间数量
from pprint import pprint

SAMPLE_COUNT = 500
SECTION = 'section'
SUM = 'sum'

# 在start和end两个日期之间生成一个随机日期
def random_date(start, end):
    delta = end - start
    seconds_delta = (delta.days * 24 * 60 * 60)  # 开始日期和结束日期之间相差的秒数
    random_seconds = randrange(seconds_delta)
    return start + timedelta(seconds=random_seconds)

def my_time():
    times = []
    for i in range(7):
        times.append({SECTION: i, SUM: 0})
    # pprint(times)

    start_date = datetime.datetime(2000, 1, 1)
    end_date = datetime.datetime(2021, 12, 31)

    cnt = 0
    while cnt < SAMPLE_COUNT:
        random_d = random_date(start_date, end_date)
        weekday = random_d.weekday()  # 0是星期一,6是星期日
        # pprint(f'{random_d.strftime("%Y-%m-%d")} {number_to_weekday(weekday)}')

        for tx in times:
            if tx[SECTION] == weekday:
                tx[SUM] = tx[SUM] + 1
                break

        cnt = cnt + 1

    return times

def drawchart(df):
    myfont = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    plt.rc('font', family='YaHei', weight='bold')

    order = []
    name = []
    mem = []
    for d, i in zip(df.values, df.index):
        order.append(i)
        name.append(d[0])
        mem.append(int(d[1]))

    FONT_SIZE = 12

    fig, ax = plt.subplots(figsize=(15, 13))

    b = ax.barh(y=range(len(name)), width=mem, align='center', color='red')

    # 为横向水平的柱图右侧添加数据标签。
    i = 0
    for rect in b:
        w = rect.get_width()
        ax.text(x=w, y=rect.get_y() + rect.get_height() / 2, s='%d' % (int(w)),
                horizontalalignment='left', verticalalignment='center',
                fontproperties=myfont, fontsize=FONT_SIZE - 2, color='green')
        ax.text(x=w / 2, y=rect.get_y() + rect.get_height() / 2, s=str(order[i]),
                horizontalalignment='center', verticalalignment='center',
                fontproperties=myfont, fontsize=FONT_SIZE - 3, color='white')
        i = i + 1

    ax.set_yticks(range(len(name)))
    ax.set_yticklabels(name, fontsize=FONT_SIZE - 1, fontproperties=myfont)

    ax.invert_yaxis()

    ax.set_xlabel('数据', fontsize=FONT_SIZE + 2, fontproperties=myfont)
    ax.set_title('不同星期天的数据点总量统计排名', fontsize=FONT_SIZE + 2, fontproperties=myfont)

    # 不要横坐标上的label标签。
    plt.xticks(())

    # 清除四周的边框线
    ax.get_yaxis().set_visible(True)
    for spine in ["left", "top", "right", "bottom"]:
        ax.spines[spine].set_visible(False)

    plt.subplots_adjust(left=0.15)  # 调整左侧边距

    # ax.margins(y=0.01) #缩放 zoom in

    ax.set_aspect('auto')

    plt.show()

# 把数字0,1,2,3,4,5,6转换为星期*
# 0为星期一,6为星期日,依次类推
def number_to_weekday(number):
    zh = ['一', '二', '三', '四', '五', '六', '日']
    weekday = f'星期{zh[number]}'
    return weekday

def data_to_char():
    times = my_time()
    # pprint(times)

    # 数据组装成pandas数据帧。
    pd_data = []
    for t in times:
        l = [number_to_weekday(t[SECTION]), t[SUM]]
        pd_data.append(l)

    col = ['星期*', '次数']
    df = pd.DataFrame(data=pd_data, columns=col)
    df = df.sort_values(by=col[1], axis=0, ascending=False)  # 降序

    # 重置索引
    df = df.reset_index(drop=True)
    df.index = df.index + 1

    pprint(df.head(7))

    drawchart(df)

if __name__ == '__main__':
    data_to_char()

输出:

星期*  次数
1  星期四  78
2  星期二  74
3  星期三  74
4  星期五  73
5  星期日  70
6  星期六  68
7  星期一  63

统计图表:

相关文章