1111
1212import plotly .graph_objs as go
1313from plotly .matplotlylib .mplexporter import Renderer
14+ from plotly .matplotlylib .mplexporter .utils import export_color
1415from plotly .matplotlylib import mpltools
1516
1617
@@ -20,11 +21,15 @@ def _export_color(color):
2021 matplotlib uses "none" for fully transparent colors, which plotly does not
2122 accept, so transparent colors are exported as transparent black.
2223 Colors already exported by the mplexporter (hex or rgba strings) are
23- passed through unchanged.
24+ passed through unchanged; raw matplotlib colors are converted with the
25+ mplexporter's export_color.
2426 """
2527 if isinstance (color , str ):
2628 return "rgba(0,0,0,0)" if color == "none" else color
27- return [_export_color (c ) for c in color ]
29+ if isinstance (color , (list , tuple )) and all (isinstance (c , str ) for c in color ):
30+ return [_export_color (c ) for c in color ]
31+ bgcolor = export_color (color )
32+ return "rgba(0,0,0,0)" if bgcolor == "none" else bgcolor
2833
2934
3035class PlotlyRenderer (Renderer ):
@@ -101,6 +106,9 @@ def open_figure(self, fig, props):
101106 autosize = False ,
102107 hovermode = "closest" ,
103108 )
109+ self .plotly_fig ["layout" ].paper_bgcolor = _export_color (
110+ fig .patch .get_facecolor ()
111+ )
104112 self .mpl_x_bounds , self .mpl_y_bounds = mpltools .get_axes_bounds (fig )
105113 margin = go .layout .Margin (
106114 l = int (self .mpl_x_bounds [0 ] * self .plotly_fig ["layout" ]["width" ]),
@@ -166,6 +174,8 @@ def open_axes(self, ax, props):
166174 ]
167175 self .current_bars = []
168176 self .axis_ct += 1
177+ # update plot background with the axes background from mpl
178+ self .plotly_fig ["layout" ].plot_bgcolor = _export_color (props ["axesbg" ])
169179 # set defaults in axes
170180 xaxis = go .layout .XAxis (
171181 anchor = "y{0}" .format (self .axis_ct ), zeroline = False , ticks = "inside"
0 commit comments