Python 代码实现生命之轮Wheel of life

打印 上一主题 下一主题

主题 906|帖子 906|积分 2718

  近来看一个生命之轮的视频,让我们珍惜时间,因为一生是有限的。利用Python创建生命倒计时图表,珍惜时间,活在当下。
生命之轮(Wheel of life),这一概念最初由 Success Motivation® Institute, Inc. 的首创人 Paul J. Meyer 提出,生命之轮使人可以或许根据此刻的价值观、愿景和优先事项,规划ta将为ta生活的每个范畴付出的时间量。

✍️ 要创造和利用生命之轮,应该遵循以下步骤:

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

 绿色的圆点表示以及一去不返的过去,空心圆圈表示剩余的时间,设置目标年龄80岁,每行展示4年。每过一个月就涂掉一个圆圈。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表