init
This commit is contained in:
100
3_pie_suggested_lifestyle.py
Normal file
100
3_pie_suggested_lifestyle.py
Normal file
@@ -0,0 +1,100 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from matplotlib.patches import Rectangle
|
||||
|
||||
# Data for the pie chart
|
||||
labels = ['Tekanan Darah Sistolik', 'Berat Badan', 'Tekanan Darah Diastolik']
|
||||
en_labels = ['Systolic Blood Pressure', 'Weight', 'Diastolic Blood Pressure']
|
||||
values = [66.8, 18.9, 14.2]
|
||||
colors = ['#F77F7F', '#FFB84C', '#5ECCC4'] # Red, Orange, Teal
|
||||
|
||||
# Data for the table
|
||||
current_readings = [132, 70, 88]
|
||||
target_values = ['< 130', '41,6-51,5', '< 85']
|
||||
units = ['mmHg', 'kg', 'mmHg']
|
||||
|
||||
# Create figure with subplots
|
||||
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10),
|
||||
gridspec_kw={'height_ratios': [1, 0.8]})
|
||||
plt.subplots_adjust(hspace=0.3)
|
||||
|
||||
# Pie Chart
|
||||
wedges, texts, autotexts = ax1.pie(values, colors=colors, autopct='',
|
||||
startangle=90, wedgeprops={'edgecolor': 'white', 'linewidth': 1})
|
||||
|
||||
# Title
|
||||
ax1.set_title('Kontributor Risiko Relatif\nRelative Risk Contributors', fontsize=16, fontweight='bold', pad=20)
|
||||
|
||||
# Add custom labels outside the pie
|
||||
for i, p in enumerate(wedges):
|
||||
ang = (p.theta2 - p.theta1)/2. + p.theta1
|
||||
ang_rad = ang * np.pi / 180
|
||||
x = 1.05 * np.cos(ang_rad)
|
||||
y = 1.05 * np.sin(ang_rad)
|
||||
|
||||
# Alignment based on angle
|
||||
horizontalalignment = 'left' if x > 0 else 'right'
|
||||
|
||||
# Create the label text with percentage and name
|
||||
label = f"{values[i]}% {labels[i]}"
|
||||
|
||||
# Add the label
|
||||
ax1.annotate(label, xy=(x, y), xytext=(1*x, 1*y),
|
||||
horizontalalignment=horizontalalignment, fontsize=12)
|
||||
|
||||
# Equal aspect ratio ensures the pie chart is circular
|
||||
ax1.set_aspect('equal')
|
||||
ax1.axis('off')
|
||||
|
||||
# Table
|
||||
# Hide axes for the table subplot
|
||||
ax2.axis('off')
|
||||
|
||||
# Create table headers
|
||||
col_labels = ['Kontributor Risiko\nRisk Contributor', 'Bacaan saat ini\nCurrent reading',
|
||||
'Nilai Target\nTarget value', 'Satuan\nUnits']
|
||||
table_data = []
|
||||
|
||||
# Prepare colored boxes for the legend
|
||||
for i, (label, en_label, value, target, unit) in enumerate(zip(labels, en_labels, current_readings, target_values, units)):
|
||||
color_box = f'\n{label}\n{en_label}'
|
||||
table_data.append([color_box, value, target, unit])
|
||||
|
||||
# Create table
|
||||
table = ax2.table(cellText=table_data, colLabels=col_labels, loc='center',
|
||||
cellLoc='center', colWidths=[0.4, 0.2, 0.2, 0.2])
|
||||
|
||||
# Format table
|
||||
table.auto_set_font_size(False)
|
||||
table.set_fontsize(10)
|
||||
table.scale(1, 2.15)
|
||||
|
||||
# Add color boxes to the first column of each data row
|
||||
for i in range(len(table_data)):
|
||||
cell = table[i+1, 0] # +1 because header is row 0
|
||||
cell.get_text().set_color('black')
|
||||
cell.set_facecolor(colors[i])
|
||||
cell.set_text_props(weight='bold')
|
||||
|
||||
# Add horizontal lines
|
||||
for i in range(len(table_data)+1):
|
||||
for j in range(len(col_labels)):
|
||||
table[i, j].set_edgecolor('black')
|
||||
|
||||
# Add risk information at the bottom
|
||||
risk_text = 'Risiko Hipertensi dalam 4 Tahun\nHypertension Risk in 4 Years'
|
||||
current_risk = '51,2 %'
|
||||
target_risk = '8,3 %'
|
||||
|
||||
ax2.text(0.00, 0.1, risk_text, transform=ax2.transAxes,
|
||||
color='red', fontsize=12, fontweight='bold')
|
||||
ax2.text(0.45, 0.15, current_risk, transform=ax2.transAxes,
|
||||
color='red', fontsize=12, fontweight='bold')
|
||||
ax2.text(0.68, 0.15, target_risk, transform=ax2.transAxes,
|
||||
color='black', fontsize=12)
|
||||
|
||||
filename = '3_pie_suggested_lifestyle.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filename, dpi=300, bbox_inches='tight')
|
||||
print(f"Image saved as {filename}")
|
||||
Reference in New Issue
Block a user