Files
pyplot-health-analysis/2_urinalis.py
AlfandiMario 78a26bc2d0 init
2025-03-04 11:40:35 +07:00

63 lines
2.1 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import FancyBboxPatch, Rectangle
from matplotlib.colors import LinearSegmentedColormap
# Color scheme
green = '#8DC58D'
yellow = '#FBBC78'
red = '#F77F7F'
gray = '#DDDDDD'
light_blue = '#D9E8F5'
light_gray = '#F5F5F5'
SEGMENT_HEIGHT = 0.1
Y_MARKER = 0.6 * SEGMENT_HEIGHT
Y_LABEL = 0.2 * SEGMENT_HEIGHT
# Data
categories = ["pH",
"Berat Jenis Urin (Urine Specific Gravity)"]
values = [8.0, 1.010]
ranges = [(0, 14, [4.8, 7.4]), (1.005, 1.030, [1.015, 1.025])]
colors = [[yellow, green, red], # pH
[yellow, green, red]] # Urin]
fig, axes = plt.subplots(len(categories), 1, figsize=(12, 2.6))
for i, ax in enumerate(axes):
min_val, max_val, thresholds = ranges[i]
value = values[i]
# Create segments
# y_gap = 0.01 # {{ Removed y_gap definition }}
ax.set_ylim(0, 0.1) # {{ Updated y-axis limits to remove y_gap }}
all_points = [min_val] + thresholds + [max_val]
for j in range(len(all_points)-1):
start = all_points[j]
end = all_points[j+1]
segment_width = end - start
rect = Rectangle((start, 0), segment_width, SEGMENT_HEIGHT, # {{ Updated y-coordinate and height to remove y_gap }}
facecolor=colors[i][j % len(colors[i])], linewidth=0)
ax.add_patch(rect)
# Plot value marker
ax.scatter([value], [Y_MARKER], color="white", marker="o", s=100, zorder=3, edgecolor='gray')
ax.text(value, Y_LABEL, f"{value:.1f}", ha="center", fontsize=10, color="black", weight="bold",
fontfamily='monospace')
# Axis formatting - properly align ticks with segment boundaries
ax.set_xlim(min_val, max_val)
ax.set_yticks([])
ax.set_xticks(all_points) # Set ticks at exact boundary points
ax.set_title(categories[i], fontsize=12, weight="bold", fontfamily='monospace')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
filename = '2_urinalis.png'
plt.tight_layout()
plt.savefig(filename, dpi=300, bbox_inches='tight')
print(f"Image saved as {filename}")