From a3188de9a70a5a332de30d32c63c59b590f8d5c5 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/__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 c95de522477..cc3183ea2bf 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" From 18abcb60236b8ba2c9801e5307c64a53ec82c9a3 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/renderer.py | 4 ++-- plotly/matplotlylib/tests/test_renderer.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/plotly/matplotlylib/renderer.py b/plotly/matplotlylib/renderer.py index cc3183ea2bf..8f01558ba2d 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -300,7 +300,7 @@ def draw_bar(self, coll): ) # TODO ditto if len(bar["x"]) > 1: self.msg += " Heck yeah, I drew that bar chart\n" - (self.plotly_fig.add_trace(bar),) + self.plotly_fig.add_trace(bar) if bar_gap is not None: self.plotly_fig["layout"]["bargap"] = bar_gap else: @@ -498,7 +498,7 @@ def draw_marked_line(self, **props): marked_line["x"] = mpltools.mpl_dates_to_datestrings( marked_line["x"], formatter ) - (self.plotly_fig.add_trace(marked_line),) + self.plotly_fig.add_trace(marked_line) self.msg += " Heck yeah, I drew that line\n" elif props["coordinates"] == "axes": # dealing with legend graphical elements diff --git a/plotly/matplotlylib/tests/test_renderer.py b/plotly/matplotlylib/tests/test_renderer.py index f46ecb796f7..a519ba86326 100644 --- a/plotly/matplotlylib/tests/test_renderer.py +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -2,6 +2,7 @@ from . import plt + def test_plot_bgcolor_defaults_to_white(): plt.figure() plt.plot([0, 1], [0, 1])