近来看一个生命之轮的视频,让我们珍惜时间,因为一生是有限的。利用Python创建生命倒计时图表,珍惜时间,活在当下。
生命之轮(Wheel of life),这一概念最初由 Success Motivation® Institute, Inc. 的首创人 Paul J. Meyer 提出,生命之轮使人可以或许根据此刻的价值观、愿景和优先事项,规划ta将为ta生活的每个范畴付出的时间量。
✍️ 要创造和利用生命之轮,应该遵循以下步骤:
1、确定你人生的重点范畴
2、利用你选择的类别创建一个轮子
3、评价每个范畴
4、连接这些打过分的点
5、将结果与你理想的状况进行比较
6、采取步骤解决你想要改进的范畴
创建生命倒计时代码如下:- 1 """
- 2 导入必需的库:
- 3 matplotlib.pyplot用于绘图,
- 4 numpy用于数值计算,
- 5 datetime用于获取当前日期
- 6
- 7 """
- 8 import matplotlib.pyplot as plt
- 9 import numpy as np
- 10 from datetime import datetime
- 11
- 12 # 出生年月日
- 13 birth_year = 1991
- 14 birth_month = 9
- 15 birth_day = 1
- 16
- 17 # 当前年月日
- 18 current_date = datetime.now()
- 19 current_year = current_date.year
- 20 current_month = current_date.month
- 21
- 22 # 设置图表的总年数和每行的年数
- 23 # 设置图表的总年数为80年,每行显示4年
- 24 total_years = 80
- 25 years_per_row = 4
- 26
- 27 # 计算总行数和总列数
- 28 total_rows = total_years // years_per_row
- 29 total_columns = years_per_row * 12
- 30
- 31 # 创建图表
- 32 fig, ax = plt.subplots(figsize=(12, 8))
- 33
- 34 # 计算从出生到当前日期已经过去的月份数
- 35 months_passed = (current_year - birth_year) * 12 + (current_month - birth_month)
- 36
- 37 # 绘制所有月份
- 38 # for i in range(total_rows * total_columns):
- 39 # color = 'red' if i < months_passed else 'black'
- 40 # ax.scatter(i % total_columns, i // total_columns, marker='o', edgecolors=color, facecolors='none', s=30)
- 41
- 42
- 43 # 绘制所有月份
- 44 # 如果该月份已经过去,用红色边框和绿色填充来表示,否则用黑色边框和空心来表示。
- 45 for i in range(total_rows * total_columns):
- 46 if i < months_passed:
- 47 ax.scatter(i % total_columns, i // total_columns, marker='o', edgecolors='red', facecolors='green', s=150)
- 48 else:
- 49 ax.scatter(i % total_columns, i // total_columns, marker='o', edgecolors='black', facecolors='none', s=150)
- 50
- 51 # 添加每12列之后的分割线
- 52 """
- 53 通过plt.axvline()函数在每12列之后添加一条分割线。
- 54 x=col - 0.5表示分割线的位置,
- 55 color='gray'设置分割线的颜色为灰色,
- 56 linestyle='--'设置分割线为虚线,
- 57 linewidth=1设置分割线的宽度为1。
- 58 这样可以在每行显示的4年的12个月份之间添加分割线,使图表更清晰。
- 59 """
- 60 for col in range(12, total_columns, 12):
- 61 plt.axvline(x=col - 0.5, color='gray', linestyle='--', linewidth=1)
- 62
- 63 # 设置轴标签
- 64 ax.set_xlabel('Months')
- 65 ax.set_ylabel('Years')
- 66
- 67 # 设置轴刻度
- 68 """
- 69
- 70 ax.set_xticks(np.arange(0, total_columns, 12))
- 71 ax.set_xticklabels(np.arange(1, years_per_row + 1))
- 72 """
- 73
- 74 """
- 75 ax.set_xticks(np.arange(0, total_columns + 1, 1))设置了X轴的刻度,使其每列都显示刻度线,
- 76 而xtick_labels使用np.tile函数重复生成1到12的标签。
- 77 这样可以在每个1到4的列中分别显示1到12的刻度值。
- 78 """
- 79 ax.set_xticks(np.arange(0, total_columns, 1))
- 80 xtick_labels = np.tile(np.arange(1, 13), 4)
- 81 # print(xtick_labels)
- 82 ax.set_xticklabels(xtick_labels)
- 83
- 84
- 85
- 86 # 设置Y轴刻度
- 87 ax.set_yticks(np.arange(0, total_rows, 1))
- 88 ax.set_yticklabels(np.arange(0, total_years, years_per_row))
- 89
- 90 # 设置标题
- 91 ax.set_title('A 80-Year Human Life in Months')
- 92
- 93 # 隐藏右边和上边的轴线
- 94 ax.spines['right'].set_visible(False)
- 95 ax.spines['top'].set_visible(False)
- 96
- 97 # 显示网格
- 98 # ax.grid(True)
- 99
- 100 # 反转y轴,使得0岁在顶部
- 101 plt.gca().invert_yaxis()
- 102
- 103 # 显示图表
- 104 plt.tight_layout()
- 105 plt.savefig("WhellOfLife.png")
- 106 plt.show()
复制代码 展示如下:

绿色的圆点表示以及一去不返的过去,空心圆圈表示剩余的时间,设置目标年龄80岁,每行展示4年。每过一个月就涂掉一个圆圈。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |