Skip to content

Commit b2cab76

Browse files
authored
Merge pull request #5285 from robertoffmoura/rm/default-plot_bgcolor-white
Use matplotlib background colors when converting from matplotlib
2 parents 15f33d2 + 836c96a commit b2cab76

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Added
88
- Support `marginal_x`/`marginal_y="heatmap"` in `density_heatmap`, drawing a single-row/column heatmap strip in the margin colored by the same `z`/`histfunc` aggregate as the main plot and sharing its color scale [[#5706](https://github.com/plotly/plotly.py/issues/5706)], with thanks to @lucasjamar for the contribution!
99

10+
### Fixed
11+
- Fix `mpl_to_plotly` not setting `paper_bgcolor` and `plot_bgcolor` from the matplotlib figure and axes backgrounds, so converted figures match the source figure's background colors [[#5285](https://github.com/plotly/plotly.py/pull/5285)], with thanks to @robertoffmoura for the contribution!
12+
1013
## [7.0.0] - 2026-08-25
1114

1215
### Fixed

plotly/matplotlylib/mplexporter/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def get_figure_properties(fig):
271271
"figwidth": fig.get_figwidth(),
272272
"figheight": fig.get_figheight(),
273273
"dpi": fig.dpi,
274+
"figbg": export_color(fig.patch.get_facecolor()),
274275
}
275276

276277

plotly/matplotlylib/renderer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def open_figure(self, fig, props):
101101
autosize=False,
102102
hovermode="closest",
103103
)
104+
self.plotly_fig["layout"].paper_bgcolor = _export_color(props["figbg"])
104105
self.mpl_x_bounds, self.mpl_y_bounds = mpltools.get_axes_bounds(fig)
105106
margin = go.layout.Margin(
106107
l=int(self.mpl_x_bounds[0] * self.plotly_fig["layout"]["width"]),
@@ -166,6 +167,8 @@ def open_axes(self, ax, props):
166167
]
167168
self.current_bars = []
168169
self.axis_ct += 1
170+
# update plot background with the axes background from mpl
171+
self.plotly_fig["layout"].plot_bgcolor = _export_color(props["axesbg"])
169172
# set defaults in axes
170173
xaxis = go.layout.XAxis(
171174
anchor="y{0}".format(self.axis_ct), zeroline=False, ticks="inside"

plotly/matplotlylib/tests/test_renderer.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,37 @@ def test_filled_path_collection_date_xaxis():
199199
filled = [t for t in plotly_fig.data if t.fill == "toself"]
200200
assert len(filled) >= 1
201201
assert all(isinstance(x, str) for x in filled[0].x)
202+
203+
204+
def test_background_colors_from_matplotlib_defaults():
205+
fig, ax = plt.subplots()
206+
ax.plot([0, 1], [0, 1])
207+
208+
plotly_fig = tls.mpl_to_plotly(fig)
209+
210+
assert plotly_fig.layout.plot_bgcolor == "#FFFFFF"
211+
assert plotly_fig.layout.paper_bgcolor == "#FFFFFF"
212+
213+
214+
def test_custom_background_colors_are_preserved():
215+
fig, ax = plt.subplots()
216+
fig.patch.set_facecolor("lightyellow")
217+
ax.set_facecolor("lightgray")
218+
ax.plot([0, 1], [0, 1])
219+
220+
plotly_fig = tls.mpl_to_plotly(fig)
221+
222+
assert plotly_fig.layout.plot_bgcolor == "#D3D3D3"
223+
assert plotly_fig.layout.paper_bgcolor == "#FFFFE0"
224+
225+
226+
def test_semitransparent_axes_background_preserved():
227+
"""Axes backgrounds with alpha export as mpl-style rgba strings, which
228+
must be passed through as-is, not re-parsed by export_color."""
229+
fig, ax = plt.subplots()
230+
ax.set_facecolor((0.1, 0.2, 0.3, 0.4))
231+
ax.plot([0, 1], [0, 1])
232+
233+
plotly_fig = tls.mpl_to_plotly(fig)
234+
235+
assert plotly_fig.layout.plot_bgcolor == "rgba(26, 51, 76, 0.4)"

0 commit comments

Comments
 (0)