adjust scale of plots
Browse files
app.py
CHANGED
|
@@ -41,8 +41,8 @@ def _save_to_cache(key, results_text, fig1, fig2):
|
|
| 41 |
# Save plots as PNGs
|
| 42 |
fig1_path = cache_path / "fig1.png"
|
| 43 |
fig2_path = cache_path / "fig2.png"
|
| 44 |
-
fig1.savefig(fig1_path)
|
| 45 |
-
fig2.savefig(fig2_path)
|
| 46 |
plt.close(fig1) # Close figures to free memory
|
| 47 |
plt.close(fig2)
|
| 48 |
|
|
@@ -83,11 +83,13 @@ def _load_from_cache(key):
|
|
| 83 |
ax1_recreated = fig1_recreated.add_subplot(111)
|
| 84 |
ax1_recreated.imshow(fig1_img)
|
| 85 |
ax1_recreated.axis('off') # Hide axes for image display
|
|
|
|
| 86 |
|
| 87 |
fig2_recreated = plt.figure()
|
| 88 |
ax2_recreated = fig2_recreated.add_subplot(111)
|
| 89 |
ax2_recreated.imshow(fig2_img)
|
| 90 |
ax2_recreated.axis('off') # Hide axes for image display
|
|
|
|
| 91 |
|
| 92 |
return "Loaded from cache!", metadata["results_text"], fig1_recreated, fig2_recreated
|
| 93 |
|
|
@@ -274,12 +276,35 @@ def run_simulation(
|
|
| 274 |
ax2.plot(range(num_years + 1), path_data, alpha=0.1, color='blue' if path_data[-1] > 0 else 'red')
|
| 275 |
|
| 276 |
ax2.set_title(f'Sample Portfolio Paths for {chosen_swr_for_path_plot*100:.2f}% SWR (100 simulations shown)')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
ax2.set_xlabel('Year')
|
| 278 |
ax2.set_ylabel('Portfolio Value ($)')
|
| 279 |
-
ax2.set_yscale('log')
|
| 280 |
ax2.grid(True, which="both", ls="-", alpha=0.5)
|
| 281 |
-
ax2.axhline(y=initial_investment, color='k', linestyle='--', label=f'Initial: ${initial_investment:,.0f}')
|
| 282 |
-
ax2.axhline(y=1, color='grey', linestyle=':', label='$1 (for log scale visibility near zero)')
|
| 283 |
ax2.legend()
|
| 284 |
else:
|
| 285 |
ax2.text(0.5, 0.5, f"No portfolio paths were stored for SWR {chosen_swr_for_path_plot*100:.2f}% to plot individual simulations.",
|
|
|
|
| 41 |
# Save plots as PNGs
|
| 42 |
fig1_path = cache_path / "fig1.png"
|
| 43 |
fig2_path = cache_path / "fig2.png"
|
| 44 |
+
fig1.savefig(fig1_path, bbox_inches='tight', pad_inches=0)
|
| 45 |
+
fig2.savefig(fig2_path, bbox_inches='tight', pad_inches=0)
|
| 46 |
plt.close(fig1) # Close figures to free memory
|
| 47 |
plt.close(fig2)
|
| 48 |
|
|
|
|
| 83 |
ax1_recreated = fig1_recreated.add_subplot(111)
|
| 84 |
ax1_recreated.imshow(fig1_img)
|
| 85 |
ax1_recreated.axis('off') # Hide axes for image display
|
| 86 |
+
fig1_recreated.subplots_adjust(left=0, right=1, top=1, bottom=0) # Make axes fill the figure
|
| 87 |
|
| 88 |
fig2_recreated = plt.figure()
|
| 89 |
ax2_recreated = fig2_recreated.add_subplot(111)
|
| 90 |
ax2_recreated.imshow(fig2_img)
|
| 91 |
ax2_recreated.axis('off') # Hide axes for image display
|
| 92 |
+
fig2_recreated.subplots_adjust(left=0, right=1, top=1, bottom=0) # Make axes fill the figure
|
| 93 |
|
| 94 |
return "Loaded from cache!", metadata["results_text"], fig1_recreated, fig2_recreated
|
| 95 |
|
|
|
|
| 276 |
ax2.plot(range(num_years + 1), path_data, alpha=0.1, color='blue' if path_data[-1] > 0 else 'red')
|
| 277 |
|
| 278 |
ax2.set_title(f'Sample Portfolio Paths for {chosen_swr_for_path_plot*100:.2f}% SWR (100 simulations shown)')
|
| 279 |
+
# Collect all portfolio values for dynamic y-axis adjustment
|
| 280 |
+
all_portfolio_values = []
|
| 281 |
+
for path_data in paths_to_plot_sample:
|
| 282 |
+
all_portfolio_values.extend([val for val in path_data if val > 0]) # Only include positive values for log scale consideration
|
| 283 |
+
|
| 284 |
+
if all_portfolio_values:
|
| 285 |
+
min_val = min(all_portfolio_values)
|
| 286 |
+
max_val = max(all_portfolio_values)
|
| 287 |
+
|
| 288 |
+
# Add a small buffer to the min/max for better visualization
|
| 289 |
+
# Ensure min_val is not too close to zero for log scale
|
| 290 |
+
y_min = max(1, min_val * 0.9) if min_val > 0 else 1 # Ensure y_min is at least 1 for log scale
|
| 291 |
+
y_max = max_val * 1.1
|
| 292 |
+
|
| 293 |
+
ax2.set_ylim(y_min, y_max)
|
| 294 |
+
ax2.set_yscale('log') # Keep log scale if all values are positive
|
| 295 |
+
ax2.axhline(y=initial_investment, color='k', linestyle='--', label=f'Initial: ${initial_investment:,.0f}')
|
| 296 |
+
ax2.axhline(y=1, color='grey', linestyle=':', label='$1 (for log scale visibility near zero)')
|
| 297 |
+
else:
|
| 298 |
+
# Fallback if no positive values are found (e.g., all simulations failed immediately)
|
| 299 |
+
ax2.set_yscale('linear') # Switch to linear scale if no positive values
|
| 300 |
+
ax2.set_ylim(0, initial_investment * 1.1) # Default linear range
|
| 301 |
+
ax2.axhline(y=initial_investment, color='k', linestyle='--', label=f'Initial: ${initial_investment:,.0f}')
|
| 302 |
+
ax2.axhline(y=0, color='grey', linestyle=':', label='$0')
|
| 303 |
+
|
| 304 |
+
|
| 305 |
ax2.set_xlabel('Year')
|
| 306 |
ax2.set_ylabel('Portfolio Value ($)')
|
|
|
|
| 307 |
ax2.grid(True, which="both", ls="-", alpha=0.5)
|
|
|
|
|
|
|
| 308 |
ax2.legend()
|
| 309 |
else:
|
| 310 |
ax2.text(0.5, 0.5, f"No portfolio paths were stored for SWR {chosen_swr_for_path_plot*100:.2f}% to plot individual simulations.",
|