From cc2aa8389e3a5175dfa702e74fa231b831f22be8 Mon Sep 17 00:00:00 2001 From: robertoffmoura Date: Fri, 18 Jul 2025 16:11:33 +0100 Subject: [PATCH 1/2] Set plot_bgcolor to white by default when converting from matplotlib figure --- plotly/matplotlylib/renderer.py | 1 + plotly/matplotlylib/tests/test_renderer.py | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/plotly/matplotlylib/renderer.py b/plotly/matplotlylib/renderer.py index d55f86ef3c..024bbc972b 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -87,6 +87,7 @@ def open_figure(self, fig, props): autosize=False, hovermode="closest", ) + self.plotly_fig["layout"].template.layout.plot_bgcolor = "white" self.mpl_x_bounds, self.mpl_y_bounds = mpltools.get_axes_bounds(fig) margin = go.layout.Margin( l=int(self.mpl_x_bounds[0] * self.plotly_fig["layout"]["width"]), diff --git a/plotly/matplotlylib/tests/test_renderer.py b/plotly/matplotlylib/tests/test_renderer.py index 0d63e4815b..ed983f3be2 100644 --- a/plotly/matplotlylib/tests/test_renderer.py +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -84,3 +84,12 @@ def test_multiple_traces_native_legend(): assert plotly_fig.data[0].mode == "lines" assert plotly_fig.data[1].mode == "markers" assert plotly_fig.data[2].mode == "lines+markers" + + +def test_plot_bgcolor_defaults_to_white(): + plt.figure() + plt.plot([0, 1], [0, 1]) + + plotly_fig = tls.mpl_to_plotly(plt.gcf()) + + assert plotly_fig.layout.template.layout.plot_bgcolor == "white" From 2ac7771ea408454e491ee805eb46a26b2ce543af Mon Sep 17 00:00:00 2001 From: robertoffmoura Date: Tue, 12 Aug 2025 10:03:43 +0100 Subject: [PATCH 2/2] Run ruff format --- plotly/matplotlylib/tests/test_renderer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plotly/matplotlylib/tests/test_renderer.py b/plotly/matplotlylib/tests/test_renderer.py index ed983f3be2..c5b3340f0d 100644 --- a/plotly/matplotlylib/tests/test_renderer.py +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -86,6 +86,7 @@ def test_multiple_traces_native_legend(): assert plotly_fig.data[2].mode == "lines+markers" + def test_plot_bgcolor_defaults_to_white(): plt.figure() plt.plot([0, 1], [0, 1])