Skip to content

Commit 02087e5

Browse files
Add save functionality to _MonteCarloPlots.all method
Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>
1 parent 840ef5d commit 02087e5

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

rocketpy/plots/monte_carlo_plots.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from pathlib import Path
2+
13
import matplotlib.pyplot as plt
24
import numpy as np
35
from matplotlib.transforms import offset_copy
46

57
from ..tools import generate_monte_carlo_ellipses, import_optional_dependency
8+
from .plot_helpers import show_or_save_plot
69

710

811
class _MonteCarloPlots:
@@ -159,7 +162,7 @@ def ellipses(
159162
else:
160163
plt.show()
161164

162-
def all(self, keys=None):
165+
def all(self, keys=None, *, filename=None):
163166
"""
164167
Plot the histograms of the Monte Carlo simulation results.
165168
@@ -168,6 +171,14 @@ def all(self, keys=None):
168171
keys : str, list or tuple, optional
169172
The keys of the results to be plotted. If None, all results will be
170173
plotted. Default is None.
174+
filename : str | None, optional
175+
The path the plot should be saved to. By default None, in which case
176+
the plot will be shown instead of saved. If a filename is provided,
177+
each histogram will be saved with the key name appended to the
178+
filename (e.g., "plots/histogram_apogee.png" for key "apogee" with
179+
filename "plots/histogram.png"). Supported file endings are: eps,
180+
jpg, jpeg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff and
181+
webp (these are the formats supported by matplotlib).
171182
172183
Returns
173184
-------
@@ -207,7 +218,16 @@ def all(self, keys=None):
207218
ax1.set_xticks([])
208219

209220
plt.tight_layout()
210-
plt.show()
221+
222+
# Generate the filename for this specific key if saving
223+
if filename is not None:
224+
file_path = Path(filename)
225+
key_filename = str(
226+
file_path.parent / f"{file_path.stem}_{key}{file_path.suffix}"
227+
)
228+
show_or_save_plot(key_filename)
229+
else:
230+
show_or_save_plot(None)
211231

212232
def plot_comparison(self, other_monte_carlo):
213233
"""

tests/integration/simulation/test_monte_carlo.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,34 @@ def test_monte_carlo_plots(mock_show, monte_carlo_calisto_pre_loaded):
134134
_post_test_file_cleanup()
135135

136136

137+
def test_monte_carlo_plots_all_save(monte_carlo_calisto_pre_loaded):
138+
"""Tests the plots.all method with save functionality.
139+
140+
Parameters
141+
----------
142+
monte_carlo_calisto_pre_loaded : MonteCarlo
143+
The MonteCarlo object, this is a pytest fixture.
144+
"""
145+
try:
146+
# Test saving with a single key
147+
monte_carlo_calisto_pre_loaded.plots.all(
148+
keys="apogee", filename="test_histogram.png"
149+
)
150+
assert os.path.exists("test_histogram_apogee.png")
151+
os.remove("test_histogram_apogee.png")
152+
153+
# Test saving with multiple keys
154+
monte_carlo_calisto_pre_loaded.plots.all(
155+
keys=["apogee", "x_impact"], filename="test_multi.png"
156+
)
157+
assert os.path.exists("test_multi_apogee.png")
158+
assert os.path.exists("test_multi_x_impact.png")
159+
os.remove("test_multi_apogee.png")
160+
os.remove("test_multi_x_impact.png")
161+
finally:
162+
_post_test_file_cleanup()
163+
164+
137165
def test_monte_carlo_export_ellipses_to_kml(monte_carlo_calisto_pre_loaded):
138166
"""Tests the export_ellipses_to_kml method of the MonteCarlo class.
139167

0 commit comments

Comments
 (0)