Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions plotly/matplotlylib/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"