From ebd0e91470053ce4cb0c09592da44e73bd314862 Mon Sep 17 00:00:00 2001 From: whn <142425816+Whning0513@users.noreply.github.com> Date: Mon, 22 Jun 2026 14:43:38 +0800 Subject: [PATCH] Close the monthly heatmap figure explicitly after rendering --- quantstats/_plotting/wrappers.py | 6 +++--- tests/test_plots.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/quantstats/_plotting/wrappers.py b/quantstats/_plotting/wrappers.py index 19c822ed..4aa25c3f 100644 --- a/quantstats/_plotting/wrappers.py +++ b/quantstats/_plotting/wrappers.py @@ -390,7 +390,7 @@ def snapshot( if show: _plt.show(block=False) - _plt.close() + _plt.close(fig) if not show: return fig @@ -560,7 +560,7 @@ def earnings( if show: _plt.show(block=False) - _plt.close() + _plt.close(fig) if not show: return fig @@ -1860,7 +1860,7 @@ def monthly_heatmap( if show: _plt.show(block=False) - _plt.close() + _plt.close(fig) if not show: return fig diff --git a/tests/test_plots.py b/tests/test_plots.py index 8fa5b509..42bc52bc 100644 --- a/tests/test_plots.py +++ b/tests/test_plots.py @@ -7,9 +7,11 @@ import numpy as np import tempfile import os +import matplotlib.pyplot as plt import quantstats as qs from quantstats import plots +from quantstats._plotting import wrappers as plotting_wrappers @pytest.fixture @@ -54,6 +56,21 @@ def test_yearly_returns(self, sample_returns): fig = plots.yearly_returns(sample_returns, show=False) assert fig is not None + def test_yearly_returns_sparsifies_labels_for_long_ranges(self): + """Test yearly returns uses sparser year labels once the range exceeds 10 years.""" + np.random.seed(7) + dates = pd.date_range("2010-01-01", periods=365 * 12, freq="D") + returns = pd.Series(np.random.randn(len(dates)) * 0.01, index=dates, name="Strategy") + benchmark = pd.Series(np.random.randn(len(dates)) * 0.008, index=dates, name="Benchmark") + + fig = plots.yearly_returns(returns, benchmark=benchmark, show=False) + labels = [tick.get_text() for tick in fig.axes[0].get_xticklabels()] + non_empty_labels = [label for label in labels if label] + + assert len(labels) == 12 + assert len(non_empty_labels) == 6 + assert non_empty_labels == ["2010", "2012", "2014", "2016", "2018", "2020"] + def test_histogram(self, sample_returns, sample_benchmark): """Test histogram plot.""" fig = plots.histogram(sample_returns, sample_benchmark, show=False) @@ -99,6 +116,21 @@ def test_monthly_heatmap(self, sample_returns): fig = plots.monthly_heatmap(sample_returns, show=False) assert fig is not None + def test_monthly_heatmap_closes_its_own_figure(self, sample_returns, monkeypatch): + """Test monthly heatmap closes the figure it created.""" + closed = [] + original_close = plt.close + + def spy_close(fig=None): + closed.append(fig) + return original_close(fig) + + monkeypatch.setattr(plotting_wrappers._plt, "close", spy_close) + + fig = plots.monthly_heatmap(sample_returns, show=False) + + assert fig in closed + def test_distribution(self, sample_returns): """Test distribution box plot.""" fig = plots.distribution(sample_returns, show=False)