|
11 | 11 |
|
12 | 12 | import plotly.graph_objs as go |
13 | 13 | from plotly.matplotlylib.mplexporter import Renderer |
| 14 | +from plotly.matplotlylib.mplexporter.utils import export_color |
14 | 15 | from plotly.matplotlylib import mpltools |
15 | 16 |
|
16 | 17 |
|
| 18 | +def _export_background_color(color): |
| 19 | + """Export a matplotlib patch facecolor for use as a plotly background color.""" |
| 20 | + bgcolor = export_color(color) |
| 21 | + return "rgba(0,0,0,0)" if bgcolor == "none" else bgcolor |
| 22 | + |
| 23 | + |
17 | 24 | class PlotlyRenderer(Renderer): |
18 | 25 | """A renderer class inheriting from base for rendering mpl plots in plotly. |
19 | 26 |
|
@@ -79,7 +86,9 @@ def open_figure(self, fig, props): |
79 | 86 | autosize=False, |
80 | 87 | hovermode="closest", |
81 | 88 | ) |
82 | | - self.plotly_fig["layout"].template.layout.plot_bgcolor = "white" |
| 89 | + self.plotly_fig["layout"].paper_bgcolor = _export_background_color( |
| 90 | + fig.patch.get_facecolor() |
| 91 | + ) |
83 | 92 | self.mpl_x_bounds, self.mpl_y_bounds = mpltools.get_axes_bounds(fig) |
84 | 93 | margin = go.layout.Margin( |
85 | 94 | l=int(self.mpl_x_bounds[0] * self.plotly_fig["layout"]["width"]), |
@@ -145,6 +154,10 @@ def open_axes(self, ax, props): |
145 | 154 | ] |
146 | 155 | self.current_bars = [] |
147 | 156 | self.axis_ct += 1 |
| 157 | + # update plot background with the axes background from mpl |
| 158 | + self.plotly_fig["layout"].plot_bgcolor = _export_background_color( |
| 159 | + props["axesbg"] |
| 160 | + ) |
148 | 161 | # set defaults in axes |
149 | 162 | xaxis = go.layout.XAxis( |
150 | 163 | anchor="y{0}".format(self.axis_ct), zeroline=False, ticks="inside" |
|
0 commit comments