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
21 changes: 19 additions & 2 deletions plotly/matplotlylib/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@

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


def _export_color(color):
"""Export a matplotlib color for use as a plotly color.

matplotlib uses "none" for fully transparent colors, which plotly does not
accept, so transparent colors are exported as transparent black.
"""
color = export_color(color)
return "rgba(0,0,0,0)" if color == "none" else color


class PlotlyRenderer(Renderer):
"""A renderer class inheriting from base for rendering mpl plots in plotly.

Expand Down Expand Up @@ -146,10 +157,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
23 changes: 23 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,26 @@ def test_multiple_traces_native_legend():
assert plotly_fig.data[0].mode == "lines"
assert plotly_fig.data[1].mode == "markers"
assert plotly_fig.data[2].mode == "lines+markers"



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"