Skip to content

Commit 84295e9

Browse files
authored
Merge pull request #5311 from robertoffmoura/rm/default-axis-linecolor-black
Preserve matplotlib axis line colors when converting from matplotlib
2 parents 7fcb587 + 7513b83 commit 84295e9

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66

77
### Fixed
88
- Fix `mpl_to_plotly` tick marker mirroring, axis positioning, and trace visibility for twinned and overlaid subplots (such as `twinx` and `twiny`) [[#5310](https://github.com/plotly/plotly.py/pull/5310)], with thanks to @robertoffmoura for the contribution!
9+
- Fix `mpl_to_plotly` losing matplotlib axis line colors by exporting the color of each axis's displayed spine to the plotly x and y axis `linecolor` [[#5311](https://github.com/plotly/plotly.py/pull/5311)], with thanks to @robertoffmoura for the contribution!
910
- Fix concurrent first access to lazily initialized graph object properties, which could raise `ValueError("Invalid value")` [[#5691](https://github.com/plotly/plotly.py/pull/5691)], with thanks to @hb1915 for the contribution!
1011
- Fix `mpl_to_plotly` crashing on touching bars (such as `plt.hist`) due to floating-point noise producing negative `bargap` values by clamping `bargap` to `[0, 1]` [[#5696](https://github.com/plotly/plotly.py/pull/5696)], with thanks to @robertoffmoura for the contribution!
1112
- Fix `mpl_to_plotly` drawing step plots with diagonal lines instead of vertical/horizontal step segments by mapping matplotlib step drawstyles (`steps-pre`, `steps-post`, `steps-mid`) to plotly line shapes [[#5697](https://github.com/plotly/plotly.py/pull/5697)], with thanks to @robertoffmoura for the contribution!

‎plotly/matplotlylib/renderer.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ def open_axes(self, ax, props):
210210
)
211211
xaxis.update(mpl_xaxis)
212212
yaxis.update(mpl_yaxis)
213+
x_spine = "top" if xaxis["side"] == "top" else "bottom"
214+
y_spine = "right" if yaxis["side"] == "right" else "left"
215+
xaxis["linecolor"] = _export_color(ax.spines[x_spine].get_edgecolor())
216+
yaxis["linecolor"] = _export_color(ax.spines[y_spine].get_edgecolor())
213217
bottom_spine = mpltools.get_spine_visible(ax, "bottom")
214218
top_spine = mpltools.get_spine_visible(ax, "top")
215219
left_spine = mpltools.get_spine_visible(ax, "left")

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,54 @@ def test_contourf_bands_render():
520520
assert len(filled) > 0
521521

522522

523+
def test_axis_linecolor_defaults_to_black():
524+
fig, ax = plt.subplots()
525+
ax.plot([0, 1], [0, 1])
526+
527+
plotly_fig = tls.mpl_to_plotly(fig)
528+
529+
assert plotly_fig.layout.xaxis.linecolor == "#000000"
530+
assert plotly_fig.layout.yaxis.linecolor == "#000000"
531+
532+
533+
def test_custom_axis_linecolors_are_preserved():
534+
fig, ax = plt.subplots()
535+
ax.spines["bottom"].set_color("red")
536+
ax.spines["left"].set_color("green")
537+
ax.plot([0, 1], [0, 1])
538+
539+
plotly_fig = tls.mpl_to_plotly(fig)
540+
541+
assert plotly_fig.layout.xaxis.linecolor == "#FF0000"
542+
assert plotly_fig.layout.yaxis.linecolor == "#008000"
543+
544+
545+
def test_axis_linecolor_follows_right_axis_side():
546+
fig, ax = plt.subplots()
547+
ax.plot([0, 1], [0, 1])
548+
ax2 = ax.twinx()
549+
ax2.plot([0, 1], [1, 0])
550+
ax2.spines["right"].set_color("red")
551+
552+
plotly_fig = tls.mpl_to_plotly(fig)
553+
554+
assert plotly_fig.layout.yaxis2.side == "right"
555+
assert plotly_fig.layout.yaxis2.linecolor == "#FF0000"
556+
557+
558+
def test_axis_linecolor_follows_top_axis_side():
559+
fig, ax = plt.subplots()
560+
ax.spines["bottom"].set_visible(False)
561+
ax.spines["top"].set_color("red")
562+
ax.xaxis.set_ticks_position("top")
563+
ax.plot([0, 1], [0, 1])
564+
565+
plotly_fig = tls.mpl_to_plotly(fig)
566+
567+
assert plotly_fig.layout.xaxis.side == "top"
568+
assert plotly_fig.layout.xaxis.linecolor == "#FF0000"
569+
570+
523571
def test_filled_path_collection_date_xaxis():
524572
"""Filled path collections with date x-values must export date strings,
525573
not raw matplotlib date numbers."""

0 commit comments

Comments
 (0)