仿照 github代码提交代码活跃图 Python实现全年运动活跃图

 

Python 代码如下:

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 # 假设有一年的运动数据,每天的运动次数
 5 np.random.seed(0)
 6 activity_data = np.random.randint(0, 11, size=365)
 7 
 8 # 创建一个7天 * 53周的网格
 9 days_in_week = 7
10 weeks = 53
11 activity_grid = np.zeros((days_in_week, weeks), dtype=int)
12 
13 # 将实际数据填充到网格中
14 for i in range(365):
15     day_of_week = i % 7
16     week_number = i // 7
17     activity_grid[day_of_week, week_number] = activity_data[i]
18 
19 # 创建图形和子图
20 fig, ax = plt.subplots(figsize=(15, 10))
21 
22 # 绘制运动活跃图
23 cmap = plt.cm.get_cmap('Greens')  # 使用绿色渐变色
24 cax = ax.imshow(activity_grid, cmap=cmap, aspect='auto')
25 
26 # 设置x轴和y轴标签
27 months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
28 ax.set_xticks(np.linspace(0, 52, 13)[:-1])  # 12个等间距的刻度
29 ax.set_xticklabels(months, rotation=45, ha='right')
30 ax.set_yticks(np.arange(days_in_week))
31 ax.set_yticklabels(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])
32 
33 # 添加颜色条
34 cbar = fig.colorbar(cax)
35 cbar.set_label('Number of Activities')
36 
37 # 设置标题和标签
38 ax.set_title('Annual Activity Chart for 2025')
39 ax.set_xlabel('Month')
40 ax.set_ylabel('Day of the Week')
41 
42 # 显示图形
43 plt.tight_layout()
44 plt.show()

 

效果图:

 

 

matlab代码如下:

% 读取 Excel 文件
filename = '步数.xlsx'; % 文件路径
data = readtable(filename);

% 提取日期和步数
dates = datetime(data{:, 1}, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
steps = data{:, 2};

% 确保日期从2025-02-10(周一)开始
first_date = dates(1); % 2025-02-10 是周一
first_monday = first_date; % 直接使用第一天作为起始周一

% 创建完整日期范围
last_date = dates(end);

% 初始化步数矩阵
num_weeks = ceil(days(last_date - first_monday) / 7);
step_matrix = NaN(num_weeks, 7); % 使用NaN表示无数据

% 使用日期序号计算索引
date_nums = datenum(dates);
first_num = datenum(first_monday);

% 填充步数矩阵
for i = 1:length(dates)
    % 计算日期偏移天数
    day_offset = date_nums(i) - first_num;
    
    % 计算周索引(从1开始)
    week_index = floor(day_offset / 7) + 1;
    
    % 计算星期索引(周一=1,周日=7)
    day_index = mod(day_offset, 7) + 1;
    
    % 确保索引在范围内
    if week_index >= 1 && week_index <= num_weeks && day_index >= 1 && day_index <= 7
        step_matrix(week_index, day_index) = steps(i);
    end
end

% GitHub风格颜色映射 (从浅到深的绿色)
colors = [
    0.94 0.94 0.94;  % 最浅 - 0-5k
    0.80 0.94 0.80;  % 浅绿 - 5k-10k
    0.60 0.85 0.60;  % 中浅绿 - 10k-15k
    0.40 0.76 0.40;  % 中绿 - 15k-20k
    0.25 0.67 0.25;  % 深绿 - 20k-25k
    0.10 0.55 0.10   % 最深 - 25k+
];

% 将步数分为6个等级
step_levels = [0 5000 10000 15000 20000 25000 Inf];

% 创建颜色索引
color_idx = discretize(step_matrix, step_levels);

% 绘制活跃图
figure('Color', 'white'); % 白色背景
h = imagesc(color_idx');
colormap(colors); 

% 移除坐标轴
set(gca, 'XTick', [], 'YTick', [], 'XColor', 'none', 'YColor', 'none');

% 添加星期标签
text(-0.*ones(7,1), 1:7, {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'}, ...
    'HorizontalAlignment', 'right', 'FontSize', 9, 'FontName', 'Arial');

% 添加颜色条
c = colorbar('Location', 'southoutside', 'Ticks', 1:6);
c.Label.String = '每天步数';
c.Label.FontSize = 15;
c.TickLabels = {'0', '5000', '10000', '15000', '20000', '25000+'};
c.FontSize = 10;
set(c, 'Position', [0.35, 0.2, 0.35, 0.05]); 

% 添加标题
title('每天步数活跃图', 'FontSize', 15, 'FontWeight', 'normal');

% ====== 添加日期水印 ======
% 设置字体样式
text_options = {
    'HorizontalAlignment', 'center', ...
    'VerticalAlignment', 'middle', ...
    'FontSize', 9, ...
    'FontName', 'Arial'
};

% 遍历所有单元格添加日期水印
for week = 1:size(step_matrix, 1)
    for weekday = 1:7
        % 计算当前格子的日期
        current_date = first_monday + days((week-1)*7 + (weekday-1));
        
        % 检查日期是否在有效范围内
        if current_date <= last_date
            % 判断是否为每月1号
            if day(current_date) == 1
                % 1号显示月份缩写+日期 (如: Mar-1)
                date_str = [datestr(current_date, 'mmm'), '-', num2str(day(current_date))];
            else
                % 其他日期只显示数字
                date_str = num2str(day(current_date));
            end
            
            % 根据背景色调整文本颜色
            if ~isnan(color_idx(week, weekday)) && color_idx(week, weekday) >= 4
                text_color = [1 1 1]; % 深色背景用白色文本
            else
                text_color = [0.3 0.3 0.3]; % 浅色背景用深灰色文本
            end
            
            % 在单元格中心添加文本
            text(week, weekday, date_str, 'Color', text_color, text_options{:});
        end
    end
end

% 调整图形显示
axis equal tight;
set(gca, 'YDir', 'reverse');  % 确保周一在顶部

% 添加边框
box on;
posted @ 2025-01-03 22:45  ghzphy  阅读(53)  评论(0)    收藏  举报