add: line chart

This commit is contained in:
AlfandiMario
2025-03-04 12:43:26 +07:00
parent 78a26bc2d0
commit 38b64ef134
4 changed files with 190 additions and 12 deletions

View File

@@ -130,18 +130,6 @@
"display_name": "ai-inacbg",
"language": "python",
"name": "ai-inacbg"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,

116
4_line_skenario.ipynb Normal file

File diff suppressed because one or more lines are too long

BIN
4_line_skenario.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

74
4_line_skenario.py Normal file
View File

@@ -0,0 +1,74 @@
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
from datetime import datetime
# Data
years = [2025, 2027, 2029, 2031, 2033, 2035]
dates = [datetime(year, 2, 12) for year in years]
ages = [65, 67, 69, 71, 73, 75]
# Current trend data (red line)
current_trend = [51.2, 52.2, 53.3, 54.4, 55.5, 56.7]
# Improved trend data (green line)
improved_trend = [51.2, 38.6, 28.6, 21, 15.4, 11.3]
# Create figure and axis
fig, ax = plt.subplots(figsize=(12, 6))
# Plot lines
current_line, = ax.plot(dates, current_trend, 'o-', color='#FF7B7B', linewidth=2, markersize=8)
improved_line, = ax.plot(dates, improved_trend, 'o-', color='#9ABE64', linewidth=2, markersize=8)
# Plot the initial data point with cyan marker
ax.plot([dates[0]], [current_trend[0]], 'o', color='#59D8E6', markersize=10)
# Add values to the lines
for i, (x, y) in enumerate(zip(dates, current_trend)):
ax.text(x, y+1, f"{y}", ha='center', va='bottom', color='#FF7B7B')
for i, (x, y) in enumerate(zip(dates, improved_trend)):
ax.text(x, y-1.5, f"{y}", ha='center', va='top', color='#9ABE64')
# Set x-axis format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.xticks(dates)
# Add a second x-axis for ages
ax2 = ax.twiny()
ax2.set_xlim(ax.get_xlim())
ax2.set_xticks(ax.get_xticks())
ax2.set_xticklabels(ages)
# Set y-axis limits
ax.set_ylim(0, 75)
ax.set_yticks(range(0, 71, 10))
# Add grid
ax.grid(axis='y', linestyle='-', alpha=0.3)
# Add title
plt.suptitle('Risiko Saat Ini dan Trend Peningkatannya\nCurrent and Improved Risk Trends',
fontsize=12, fontweight='bold', y=0.98, x=0.07, ha='left', fontfamily='monospace')
# Add y-axis label
ax.set_ylabel('Risiko(%)\nRisk(%)', color='gray')
# Add legend
legend_elements = [
plt.Line2D([0], [0], marker='o', color='#FF7B7B', label='Tren saat ini\nCurrent trend', markersize=8, linewidth=2),
plt.Line2D([0], [0], marker='o', color='#9ABE64', label='Tren yang diinginkan\nImproved trend', markersize=8, linewidth=2),
plt.Line2D([0], [0], marker='o', color='#59D8E6', label='Poin data Anda\nYour data points', markersize=8, linewidth=0)
]
legend = fig.legend(handles=legend_elements, loc='upper right', ncol=3,
bbox_to_anchor=(0.99, 0.99), frameon=True)
# Adjust layout
plt.tight_layout()
plt.subplots_adjust(top=0.85)
filename = '4_line_skenario.png'
plt.savefig(filename, dpi=300, bbox_inches='tight')
print(f"Image saved as {filename}")