Skip to content

Commit 90ed8fd

Browse files
Map transparent tick label colors to rgba in tickfont
1 parent f0073e0 commit 90ed8fd

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

‎plotly/matplotlylib/mpltools.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,21 @@ def prep_ticks(ax, index, ax_type, props):
510510
return axis_dict
511511

512512

513+
def _export_color(color):
514+
"""Export a matplotlib color for use as a plotly color.
515+
516+
matplotlib uses "none" for fully transparent colors, which plotly does not
517+
accept, so transparent colors are exported as transparent black.
518+
Colors already exported by the mplexporter (hex or rgba strings) are
519+
passed through unchanged.
520+
"""
521+
if color is None:
522+
return None
523+
if isinstance(color, str):
524+
return color if color != "none" else "rgba(0,0,0,0)"
525+
return [_export_color(c) for c in color]
526+
527+
513528
def prep_xy_axis(ax, props, x_bounds, y_bounds):
514529
xaxis = dict(
515530
type=props["axes"][0]["scale"],
@@ -519,7 +534,7 @@ def prep_xy_axis(ax, props, x_bounds, y_bounds):
519534
side=props["axes"][0]["position"],
520535
tickfont=dict(
521536
size=props["axes"][0]["fontsize"],
522-
color=props["axes"][0]["fontcolor"],
537+
color=_export_color(props["axes"][0]["fontcolor"]),
523538
),
524539
)
525540
xaxis.update(prep_ticks(ax, 0, "x", props))
@@ -531,7 +546,7 @@ def prep_xy_axis(ax, props, x_bounds, y_bounds):
531546
side=props["axes"][1]["position"],
532547
tickfont=dict(
533548
size=props["axes"][1]["fontsize"],
534-
color=props["axes"][1]["fontcolor"],
549+
color=_export_color(props["axes"][1]["fontcolor"]),
535550
),
536551
)
537552
yaxis.update(prep_ticks(ax, 1, "y", props))

‎plotly/matplotlylib/renderer.py‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,7 @@
1414
from plotly.matplotlylib import mpltools
1515

1616

17-
def _export_color(color):
18-
"""Export a matplotlib color for use as a plotly color.
19-
20-
matplotlib uses "none" for fully transparent colors, which plotly does not
21-
accept, so transparent colors are exported as transparent black.
22-
Colors already exported by the mplexporter (hex or rgba strings) are
23-
passed through unchanged.
24-
"""
25-
if isinstance(color, str):
26-
return "rgba(0,0,0,0)" if color == "none" else color
27-
return [_export_color(c) for c in color]
17+
from plotly.matplotlylib.mpltools import _export_color
2818

2919

3020
class PlotlyRenderer(Renderer):

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,15 @@ def test_dark_tick_label_color_exports():
373373
plotly_fig = tls.mpl_to_plotly(fig)
374374

375375
assert plotly_fig.layout.xaxis.tickfont.color == "#FFFFFF"
376+
377+
378+
def test_transparent_tick_label_color_exports():
379+
"""Transparent tick label colors ('none') export as transparent rgba."""
380+
fig, ax = plt.subplots()
381+
ax.plot([0, 1], [0, 1])
382+
ax.tick_params(labelcolor="none")
383+
384+
plotly_fig = tls.mpl_to_plotly(fig)
385+
386+
assert plotly_fig.layout.xaxis.tickfont.color == "rgba(0,0,0,0)"
387+
assert plotly_fig.layout.yaxis.tickfont.color == "rgba(0,0,0,0)"

0 commit comments

Comments
 (0)