From c6eb782aee8670cc67102ee67d4cef7d64d3f8ba Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 21 Nov 2026 10:24:12 -0300 Subject: [PATCH 1/2] MNT: draw Monte Carlo box plots with orientation instead of vert Fixes #1230. Matplotlib 3.11 deprecates boxplot(vert=...) and 3.13 removes it, so both box plots in MonteCarloPlots now pass orientation="horizontal". That argument only exists from Matplotlib 3.10, so the floor in requirements.txt moves from 3.9 to 3.10. The new tests turn MatplotlibDeprecationWarning into an error and fail on the old calls. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW575d9C9zpS7Z1v2KXVq --- requirements.txt | 4 +- rocketpy/plots/monte_carlo_plots.py | 5 +-- .../unit/simulation/test_monte_carlo_plots.py | 41 ++++++++++++++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 5ffe8e8fd..915f32c10 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ -numpy>=1.23 # matplotlib 3.9 requires it +numpy>=1.23 # matplotlib 3.10 requires it scipy>=1.8 # first to allow numpy 1.23; bootstrap needs 1.7 -matplotlib>=3.9.0 # Released May 15th 2024 +matplotlib>=3.10.0 # first with boxplot(orientation=...) netCDF4>=1.6.4 requests pytz diff --git a/rocketpy/plots/monte_carlo_plots.py b/rocketpy/plots/monte_carlo_plots.py index aeb0f2746..8cc1716ea 100644 --- a/rocketpy/plots/monte_carlo_plots.py +++ b/rocketpy/plots/monte_carlo_plots.py @@ -415,8 +415,7 @@ def all(self, keys=None, *, filename=None): ax2 = fig.add_subplot(gs[1]) # Plot boxplot - # TODO: changes vert to orientation="horizontal" when support for Py3.9 ends - ax1.boxplot(self.monte_carlo.results[key], vert=False) + ax1.boxplot(self.monte_carlo.results[key], orientation="horizontal") ax1.set_title(f"Box Plot of {key}") ax1.set_yticks([]) @@ -468,7 +467,7 @@ def plot_comparison(self, other_monte_carlo): # Plot boxplot bp = ax1.boxplot( [other_monte_carlo.results[key], self.monte_carlo.results[key]], - vert=False, + orientation="horizontal", tick_labels=["Other", "Original"], patch_artist=True, ) diff --git a/tests/unit/simulation/test_monte_carlo_plots.py b/tests/unit/simulation/test_monte_carlo_plots.py index 6e049c0fb..d2100f21a 100644 --- a/tests/unit/simulation/test_monte_carlo_plots.py +++ b/tests/unit/simulation/test_monte_carlo_plots.py @@ -1,14 +1,17 @@ -"""Unit tests for the ellipse plots in ``rocketpy.plots.monte_carlo_plots``. +"""Unit tests for the ellipse and box plots in ``rocketpy.plots.monte_carlo_plots``. Background-map fetching is covered separately in ``test_monte_carlo_plots_background.py``. Everything here runs with ``background=None`` so no tile provider is contacted. """ +import warnings from unittest.mock import patch +import matplotlib.pyplot as plt import numpy as np import pytest +from matplotlib import MatplotlibDeprecationWarning from rocketpy.plots.monte_carlo_plots import _MonteCarloPlots from rocketpy.simulation import MonteCarlo @@ -116,3 +119,39 @@ def test_ellipses_comparison_draws_over_an_image(mock_show, square_image): # py other = MockMonteCarlo({**APOGEE, **IMPACT}, filename="other") assert monte_carlo.plots.ellipses_comparison(other, image=square_image) is None + + +def _box_plot_x_range(axes): + """The whiskers of a horizontal box plot span the data along x.""" + x_data = np.concatenate([line.get_xdata() for line in axes.lines]) + return x_data.min(), x_data.max() + + +@patch("matplotlib.pyplot.show") +def test_all_draws_horizontal_box_plots_without_deprecated_arguments(mock_show): # pylint: disable=unused-argument + """``vert`` is deprecated in Matplotlib 3.11 and removed in 3.13 (#1230).""" + monte_carlo = MockMonteCarlo(dict(APOGEE)) + + with warnings.catch_warnings(): + warnings.simplefilter("error", MatplotlibDeprecationWarning) + monte_carlo.plots.all("apogee_x") + + assert _box_plot_x_range(plt.gcf().axes[0]) == (100, 300) + plt.close("all") + + +@patch("matplotlib.pyplot.show") +def test_plot_comparison_draws_horizontal_box_plots_without_deprecated_arguments( # pylint: disable=unused-argument + mock_show, +): + """The comparison box plot takes the same ``orientation`` argument (#1230).""" + monte_carlo = MockMonteCarlo(dict(APOGEE)) + other = MockMonteCarlo(dict(APOGEE), filename="other") + monte_carlo.processed_results = other.processed_results = {"apogee_x": None} + + with warnings.catch_warnings(): + warnings.simplefilter("error", MatplotlibDeprecationWarning) + monte_carlo.plots.plot_comparison(other) + + assert _box_plot_x_range(plt.gcf().axes[0]) == (100, 300) + plt.close("all") From 05b391341cafcfda7c29d2be6b4b42e627df90d7 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 5 Dec 2026 21:39:15 -0300 Subject: [PATCH 2/2] TST: tighten Monte Carlo box plot assertions and figure cleanup --- .../unit/simulation/test_monte_carlo_plots.py | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/unit/simulation/test_monte_carlo_plots.py b/tests/unit/simulation/test_monte_carlo_plots.py index d2100f21a..22f570f84 100644 --- a/tests/unit/simulation/test_monte_carlo_plots.py +++ b/tests/unit/simulation/test_monte_carlo_plots.py @@ -39,6 +39,14 @@ def square_image_fixture(tmp_path): return str(path) +@pytest.fixture(autouse=True) +def close_figures(): + """Close any open matplotlib figures before and after each test.""" + plt.close("all") + yield + plt.close("all") + + @patch("matplotlib.pyplot.show") def test_ellipses_without_apogee_data_plots_the_impact_points(mock_show, caplog): # pylint: disable=unused-argument """A results file may hold impacts and no apogees. @@ -128,7 +136,7 @@ def _box_plot_x_range(axes): @patch("matplotlib.pyplot.show") -def test_all_draws_horizontal_box_plots_without_deprecated_arguments(mock_show): # pylint: disable=unused-argument +def test_all_draws_horizontal_box_plots_without_deprecated_arguments(mock_show): """``vert`` is deprecated in Matplotlib 3.11 and removed in 3.13 (#1230).""" monte_carlo = MockMonteCarlo(dict(APOGEE)) @@ -137,21 +145,22 @@ def test_all_draws_horizontal_box_plots_without_deprecated_arguments(mock_show): monte_carlo.plots.all("apogee_x") assert _box_plot_x_range(plt.gcf().axes[0]) == (100, 300) - plt.close("all") + mock_show.assert_called_once() @patch("matplotlib.pyplot.show") -def test_plot_comparison_draws_horizontal_box_plots_without_deprecated_arguments( # pylint: disable=unused-argument +def test_plot_comparison_draws_horizontal_box_plots_without_deprecated_arguments( mock_show, ): """The comparison box plot takes the same ``orientation`` argument (#1230).""" monte_carlo = MockMonteCarlo(dict(APOGEE)) - other = MockMonteCarlo(dict(APOGEE), filename="other") - monte_carlo.processed_results = other.processed_results = {"apogee_x": None} + other = MockMonteCarlo({"apogee_x": [400, 500, 600]}, filename="other") + monte_carlo.processed_results = {"apogee_x": None} + other.processed_results = {"apogee_x": None} with warnings.catch_warnings(): warnings.simplefilter("error", MatplotlibDeprecationWarning) monte_carlo.plots.plot_comparison(other) - assert _box_plot_x_range(plt.gcf().axes[0]) == (100, 300) - plt.close("all") + assert _box_plot_x_range(plt.gcf().axes[0]) == (100, 600) + mock_show.assert_called_once()