使用Python實現(xiàn)生命之輪Wheel of life效果
最近看一個生命之輪的視頻,讓我們珍惜時間,因為一生是有限的。使用Python創(chuàng)建生命倒計時圖表,珍惜時間,活在當(dāng)下。
生命之輪(Wheel of life),這一概念最初由 Success Motivation® Institute, Inc. 的創(chuàng)始人 Paul J. Meyer 提出,生命之輪使人能夠根據(jù)此刻的價值觀、愿景和優(yōu)先事項,規(guī)劃ta將為ta生活的每個領(lǐng)域付出的時間量。
要創(chuàng)造和使用生命之輪,應(yīng)該遵循以下步驟:
1、確定你人生的重點(diǎn)領(lǐng)域
2、使用你選擇的類別創(chuàng)建一個輪子
3、評價每個領(lǐng)域
4、連接這些打過分的點(diǎn)
5、將結(jié)果與你理想的狀況進(jìn)行比較
6、采取步驟解決你想要改進(jìn)的領(lǐng)域
創(chuàng)建生命倒計時代碼如下:
"""
導(dǎo)入必需的庫:
matplotlib.pyplot用于繪圖,
numpy用于數(shù)值計算,
datetime用于獲取當(dāng)前日期
"""
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
# 出生年月日
birth_year = 1991
birth_month = 9
birth_day = 1
# 當(dāng)前年月日
current_date = datetime.now()
current_year = current_date.year
current_month = current_date.month
# 設(shè)置圖表的總年數(shù)和每行的年數(shù)
# 設(shè)置圖表的總年數(shù)為80年,每行顯示4年
total_years = 80
years_per_row = 4
# 計算總行數(shù)和總列數(shù)
total_rows = total_years // years_per_row
total_columns = years_per_row * 12
# 創(chuàng)建圖表
fig, ax = plt.subplots(figsize=(12, 8))
# 計算從出生到當(dāng)前日期已經(jīng)過去的月份數(shù)
months_passed = (current_year - birth_year) * 12 + (current_month - birth_month)
# 繪制所有月份
# for i in range(total_rows * total_columns):
# color = 'red' if i < months_passed else 'black'
# ax.scatter(i % total_columns, i // total_columns, marker='o', edgecolors=color, facecolors='none', s=30)
# 繪制所有月份
# 如果該月份已經(jīng)過去,用紅色邊框和綠色填充來表示,否則用黑色邊框和空心來表示。
for i in range(total_rows * total_columns):
if i < months_passed:
ax.scatter(i % total_columns, i // total_columns, marker='o', edgecolors='red', facecolors='green', s=150)
else:
ax.scatter(i % total_columns, i // total_columns, marker='o', edgecolors='black', facecolors='none', s=150)
# 添加每12列之后的分割線
"""
通過plt.axvline()函數(shù)在每12列之后添加一條分割線。
x=col - 0.5表示分割線的位置,
color='gray'設(shè)置分割線的顏色為灰色,
linestyle='--'設(shè)置分割線為虛線,
linewidth=1設(shè)置分割線的寬度為1。
這樣可以在每行顯示的4年的12個月份之間添加分割線,使圖表更清晰。
"""
for col in range(12, total_columns, 12):
plt.axvline(x=col - 0.5, color='gray', linestyle='--', linewidth=1)
# 設(shè)置軸標(biāo)簽
ax.set_xlabel('Months')
ax.set_ylabel('Years')
# 設(shè)置軸刻度
"""
ax.set_xticks(np.arange(0, total_columns, 12))
ax.set_xticklabels(np.arange(1, years_per_row + 1))
"""
"""
ax.set_xticks(np.arange(0, total_columns + 1, 1))設(shè)置了X軸的刻度,使其每列都顯示刻度線,
而xtick_labels使用np.tile函數(shù)重復(fù)生成1到12的標(biāo)簽。
這樣可以在每個1到4的列中分別顯示1到12的刻度值。
"""
ax.set_xticks(np.arange(0, total_columns, 1))
xtick_labels = np.tile(np.arange(1, 13), 4)
# print(xtick_labels)
ax.set_xticklabels(xtick_labels)
# 設(shè)置Y軸刻度
ax.set_yticks(np.arange(0, total_rows, 1))
ax.set_yticklabels(np.arange(0, total_years, years_per_row))
# 設(shè)置標(biāo)題
ax.set_title('A 80-Year Human Life in Months')
# 隱藏右邊和上邊的軸線
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# 顯示網(wǎng)格
# ax.grid(True)
# 反轉(zhuǎn)y軸,使得0歲在頂部
plt.gca().invert_yaxis()
# 顯示圖表
plt.tight_layout()
plt.savefig("WhellOfLife.png")
plt.show()展示如下:

綠色的圓點(diǎn)表示以及一去不返的過去,空心圓圈表示剩余的時間,設(shè)置目標(biāo)年齡80歲,每行展示4年。每過一個月就涂掉一個圓圈。
到此這篇關(guān)于使用Python實現(xiàn)生命之輪Wheel of life效果的文章就介紹到這了,更多相關(guān)Python生命之輪內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python的set處理二維數(shù)組轉(zhuǎn)一維數(shù)組的方法示例
這篇文章主要介紹了python的set處理二維數(shù)組轉(zhuǎn)一維數(shù)組的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
Python pandas DataFrame操作的實現(xiàn)代碼
這篇文章主要介紹了Python pandas DataFrame操作的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
Python 高級教程之線程進(jìn)程和協(xié)程的代碼解析
這篇文章主要介紹了Python 高級教程之線程進(jìn)程和協(xié)程的代碼解析,包括使用線程模塊的簡單示例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
python3+openCV 獲取圖片中文本區(qū)域的最小外接矩形實例
這篇文章主要介紹了python3+openCV 獲取圖片中文本區(qū)域的最小外接矩形實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06

