diff --git a/plotly/matplotlylib/renderer.py b/plotly/matplotlylib/renderer.py index a282c67cec..095b3bffdc 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -11,6 +11,7 @@ import plotly.graph_objs as go from plotly.matplotlylib.mplexporter import Renderer +from plotly.matplotlylib.mplexporter.utils import export_color from plotly.matplotlylib import mpltools @@ -24,7 +25,10 @@ def _export_color(color): """ if isinstance(color, str): return "rgba(0,0,0,0)" if color == "none" else color - return [_export_color(c) for c in color] + if isinstance(color, (list, tuple)) and all(isinstance(c, str) for c in color): + return [_export_color(c) for c in color] + bgcolor = export_color(color) + return "rgba(0,0,0,0)" if bgcolor == "none" else bgcolor class PlotlyRenderer(Renderer): @@ -168,10 +172,16 @@ def open_axes(self, ax, props): self.axis_ct += 1 # set defaults in axes xaxis = go.layout.XAxis( - anchor="y{0}".format(self.axis_ct), zeroline=False, ticks="inside" + anchor="y{0}".format(self.axis_ct), + zeroline=False, + ticks="inside", + linecolor=_export_color(ax.spines["bottom"].get_edgecolor()), ) yaxis = go.layout.YAxis( - anchor="x{0}".format(self.axis_ct), zeroline=False, ticks="inside" + anchor="x{0}".format(self.axis_ct), + zeroline=False, + ticks="inside", + linecolor=_export_color(ax.spines["left"].get_edgecolor()), ) # update defaults with things set in mpl mpl_xaxis, mpl_yaxis = mpltools.prep_xy_axis( diff --git a/plotly/matplotlylib/tests/test_renderer.py b/plotly/matplotlylib/tests/test_renderer.py index f56d830917..c85e26e793 100644 --- a/plotly/matplotlylib/tests/test_renderer.py +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -199,3 +199,25 @@ def test_filled_path_collection_date_xaxis(): filled = [t for t in plotly_fig.data if t.fill == "toself"] assert len(filled) >= 1 assert all(isinstance(x, str) for x in filled[0].x) + + +def test_axis_linecolor_defaults_to_black(): + fig, ax = plt.subplots() + ax.plot([0, 1], [0, 1]) + + plotly_fig = tls.mpl_to_plotly(fig) + + assert plotly_fig.layout.xaxis.linecolor == "#000000" + assert plotly_fig.layout.yaxis.linecolor == "#000000" + + +def test_custom_axis_linecolors_are_preserved(): + fig, ax = plt.subplots() + ax.spines["bottom"].set_color("red") + ax.spines["left"].set_color("green") + ax.plot([0, 1], [0, 1]) + + plotly_fig = tls.mpl_to_plotly(fig) + + assert plotly_fig.layout.xaxis.linecolor == "#FF0000" + assert plotly_fig.layout.yaxis.linecolor == "#008000"