Skip to content

Commit f0073e0

Browse files
Export tick label colors to the plotly tickfont
1 parent d586d22 commit f0073e0

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎plotly/matplotlylib/mplexporter/utils.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,15 @@ def get_axis_properties(axis):
239239
# Get axis scale
240240
props["scale"] = axis.get_scale()
241241

242-
# Get major tick label size (assumes that's all we really care about!)
242+
# Get major tick label size and color (assumes that's all we really
243+
# care about!)
243244
labels = axis.get_ticklabels()
244245
if labels:
245246
props["fontsize"] = labels[0].get_fontsize()
247+
props["fontcolor"] = export_color(labels[0].get_color())
246248
else:
247249
props["fontsize"] = None
250+
props["fontcolor"] = None
248251

249252
# Get associated grid
250253
props["grid"] = get_grid_style(axis)

‎plotly/matplotlylib/mpltools.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ def prep_xy_axis(ax, props, x_bounds, y_bounds):
517517
showgrid=props["axes"][0]["grid"]["gridOn"],
518518
domain=convert_x_domain(props["bounds"], x_bounds),
519519
side=props["axes"][0]["position"],
520-
tickfont=dict(size=props["axes"][0]["fontsize"]),
520+
tickfont=dict(
521+
size=props["axes"][0]["fontsize"],
522+
color=props["axes"][0]["fontcolor"],
523+
),
521524
)
522525
xaxis.update(prep_ticks(ax, 0, "x", props))
523526
yaxis = dict(
@@ -526,7 +529,10 @@ def prep_xy_axis(ax, props, x_bounds, y_bounds):
526529
showgrid=props["axes"][1]["grid"]["gridOn"],
527530
domain=convert_y_domain(props["bounds"], y_bounds),
528531
side=props["axes"][1]["position"],
529-
tickfont=dict(size=props["axes"][1]["fontsize"]),
532+
tickfont=dict(
533+
size=props["axes"][1]["fontsize"],
534+
color=props["axes"][1]["fontcolor"],
535+
),
530536
)
531537
yaxis.update(prep_ticks(ax, 1, "y", props))
532538
return xaxis, yaxis

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,25 @@ def test_custom_date_xtickvals_given_as_numbers_are_converted():
351351
"2023-01-07 00:00:00",
352352
"2023-01-10 00:00:00",
353353
)
354+
355+
356+
def test_tick_label_color_exports():
357+
"""Tick label colors are exported to the plotly tickfont."""
358+
fig, ax = plt.subplots()
359+
ax.plot([0, 1], [0, 1])
360+
361+
plotly_fig = tls.mpl_to_plotly(fig)
362+
363+
assert plotly_fig.layout.xaxis.tickfont.color == "#000000"
364+
365+
366+
def test_dark_tick_label_color_exports():
367+
"""Dark-background tick label colors are exported to the plotly
368+
tickfont."""
369+
with plt.style.context("dark_background"):
370+
fig, ax = plt.subplots()
371+
ax.plot([0, 1], [0, 1])
372+
373+
plotly_fig = tls.mpl_to_plotly(fig)
374+
375+
assert plotly_fig.layout.xaxis.tickfont.color == "#FFFFFF"

0 commit comments

Comments
 (0)