星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib

Posted zhangphil

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib相关的知识,希望对你有一定的参考价值。

星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib(2)

https://zhangphil.blog.csdn.net/article/details/126078731https://zhangphil.blog.csdn.net/article/details/126078731

在前文基础上,这次把出现次数最多的点,用红色标记出来,以示区别。并使用一组数据测试。

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

import dateutil.parser as ps

from collections import Counter

from pprint import pprint

import numpy as np

SAMPLE_COUNT = 50


# 在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_date_time():
    date_times = []

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

    cnt = 0
    while cnt < SAMPLE_COUNT:
        r = random_date(start_date, end_date)
        date_times.append(r)
        cnt = cnt + 1

    return date_times


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


def date_to_points():
    # date_times = my_date_time()

    # 测试数,验证程序正确
    date_times = [
        ps.parse('2021-07-2 13:56:12'),  # 星期五
        ps.parse('2021-07-2 12:5:12'),  # 星期五
        ps.parse('2021-07-20 19:56:12'),  # 星期二
        ps.parse('2021-07-23 21:6:12'),  # 星期五
        ps.parse('2021-7-29 20:26:2'),  # 星期四
        ps.parse('2021-7-29 20:21:2'),  # 星期四
        ps.parse('2021-7-19 1:26:2'),  # 星期一
        ps.parse('2021-7-30 21:26:2'),  # 星期五
        ps.parse('2021-7-30 21:16:21'),  # 星期五
        ps.parse('2021-7-9 21:36:13'),  # 星期五
    ]

    hours = []
    for i in range(24):
        hours.append((i, i + 1))

    points = []
    for dt in date_times:
        for h in hours:
            if h[0] <= dt.hour < h[1]:
                point = (h[0], dt.weekday(), h, dt)  # weekday()  # 0是星期一,6是星期日
                points.append(point)
                break

    to_chart(points)


def to_chart(points):
    print('points', points)

    tx, ty = [], []
    xy = []
    for p in points:
        tx.append(p[0])
        ty.append(p[1])
        xy.append((p[0], p[1]))

    print('xy', xy)

    # 找出出现次数最多的点数
    c_xy = Counter(xy)
    print('c_xy')
    pprint(c_xy)

    # max_count = max(c_xy.keys(), key=c_xy.get)
    # print(max_count)
    new_max_counts = []
    # 选取最多的那个点
    max_count = c_xy.most_common(1)
    # 再选取前5多的点数
    max_counts = c_xy.most_common(5)

    # 去重,只有当前五点数出现次数与最多最最多那个点此次数一样多时候,收集起来。
    for mc in max_counts:
        print(mc)
        if mc[1] == max_count[0][1]:
            new_max_counts.append(mc[0])

    print('new_max_counts', new_max_counts)

    sizes = []

    colors = []
    for i in range(len(tx)):
        colors.append('gray')

    idx = 0
    for p in xy:
        for k, v in c_xy.items():
            if k == p:
                print("坐标点, 出现次".format(k, v))
                sizes.append(v * 10 + 10)

                # 如果发现点在最多次数点的集合里面,把颜色值赋红色。
                if p in new_max_counts:
                    colors[idx] = 'red'
                break

        idx = idx + 1

    plt.style.use('_mpl-gallery')

    # plot
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 中文乱码
    fig, ax = plt.subplots(figsize=(7, 5))
    plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9)  # 调整边距

    ax.scatter(tx, ty, s=sizes, c=colors, vmin=0, vmax=100)

    x_labels = []
    for i in range(24):
        x_labels.append((i, i + 1))

    y_labels = []
    for i in range(7):
        y_labels.append(number_to_weekday(i))

    plt.xticks(ticks=np.arange(start=0, stop=24, step=1), labels=x_labels, rotation=45, size=8, color='red')
    plt.yticks(ticks=np.linspace(start=0, stop=7, endpoint=False, num=7, dtype=np.int32), labels=y_labels, rotation=10)

    plt.show()


if __name__ == '__main__':
    date_to_points()

输出:

points [(13, 4, (13, 14), datetime.datetime(2021, 7, 2, 13, 56, 12)), (12, 4, (12, 13), datetime.datetime(2021, 7, 2, 12, 5, 12)), (19, 1, (19, 20), datetime.datetime(2021, 7, 20, 19, 56, 12)), (21, 4, (21, 22), datetime.datetime(2021, 7, 23, 21, 6, 12)), (20, 3, (20, 21), datetime.datetime(2021, 7, 29, 20, 26, 2)), (20, 3, (20, 21), datetime.datetime(2021, 7, 29, 20, 21, 2)), (1, 0, (1, 2), datetime.datetime(2021, 7, 19, 1, 26, 2)), (21, 4, (21, 22), datetime.datetime(2021, 7, 30, 21, 26, 2)), (21, 4, (21, 22), datetime.datetime(2021, 7, 30, 21, 16, 21)), (21, 4, (21, 22), datetime.datetime(2021, 7, 9, 21, 36, 13))]
xy [(13, 4), (12, 4), (19, 1), (21, 4), (20, 3), (20, 3), (1, 0), (21, 4), (21, 4), (21, 4)]
c_xy
Counter((21, 4): 4, (20, 3): 2, (13, 4): 1, (12, 4): 1, (19, 1): 1, (1, 0): 1)
((21, 4), 4)
((20, 3), 2)
((13, 4), 1)
((12, 4), 1)
((19, 1), 1)
new_max_counts [(21, 4)]
坐标点(13, 4), 出现1次
坐标点(12, 4), 出现1次
坐标点(19, 1), 出现1次
坐标点(21, 4), 出现4次
坐标点(20, 3), 出现2次
坐标点(20, 3), 出现2次
坐标点(1, 0), 出现1次
坐标点(21, 4), 出现4次
坐标点(21, 4), 出现4次
坐标点(21, 4), 出现4次

绘制的图:

以上是关于星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib的主要内容,如果未能解决你的问题,请参考以下文章

星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib

星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib

[星期-时间]维度,解析日期绘制散点图,python

[星期-时间]维度,解析日期绘制散点图,python

echarts图表——漏斗图&散点图

数据可视化 | 基础图表应用之散点图