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()
效果图:
标签:week,github,代码,活跃,set,plt,activity,np,ax From: https://www.cnblogs.com/ghzhan/p/18651102