From c368e89577ab9072f3fe7216861d766f30c73f79 Mon Sep 17 00:00:00 2001 From: robertoffmoura Date: Fri, 18 Jul 2025 16:11:33 +0100 Subject: [PATCH] Set plot_bgcolor to white by default when converting from matplotlib figure --- plotly/matplotlylib/renderer.py | 1 + plotly/matplotlylib/tests/__init__.py | 4 ++++ plotly/matplotlylib/tests/test_renderer.py | 11 +++++++++++ 3 files changed, 16 insertions(+) create mode 100644 plotly/matplotlylib/tests/__init__.py create mode 100644 plotly/matplotlylib/tests/test_renderer.py diff --git a/plotly/matplotlylib/renderer.py b/plotly/matplotlylib/renderer.py index e05a046607a..8878695ffe1 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -85,6 +85,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/__init__.py b/plotly/matplotlylib/tests/__init__.py new file mode 100644 index 00000000000..c29e9896d61 --- /dev/null +++ b/plotly/matplotlylib/tests/__init__.py @@ -0,0 +1,4 @@ +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt diff --git a/plotly/matplotlylib/tests/test_renderer.py b/plotly/matplotlylib/tests/test_renderer.py new file mode 100644 index 00000000000..f46ecb796f7 --- /dev/null +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -0,0 +1,11 @@ +import plotly.tools as tls + +from . import plt + +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"