Skip to content

Convert axhline, axvline, and axline to layout shapes - #5719

Open
robertoffmoura wants to merge 1 commit into
plotly:mainfrom
robertoffmoura:rm/support-axhline
Open

Convert axhline, axvline, and axline to layout shapes#5719
robertoffmoura wants to merge 1 commit into
plotly:mainfrom
robertoffmoura:rm/support-axhline

Conversation

@robertoffmoura

Copy link
Copy Markdown
Contributor

mpl_to_plotly silently drops reference lines. axhline, axvline, and axline produce lines with blended or axes transforms, which the renderer skipped with "I found a line that didn't have 'data' coordinates! I will not draw it, but I did not crash.". The converted figure ends up with the reference lines missing completely.

Fix: reference lines are now drawn as layout line shapes:

  • endpoints are mapped to data coordinates using ax.transData.inverted()
  • added as go.layout.Shape(type="line") on the current subplot
  • color, alpha, linewidth, and dash styles are preserved

Before: 0 shapes (lines dropped with a warning).
After: lines render as layout shapes spanning the axis limits.

Snippet to reproduce:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import plotly.tools as tls

cases = {
    "axhline": lambda: plt.axhline(0.5, color="red", linestyle="--"),
    "axvline": lambda: plt.axvline(2.0, color="blue", linestyle="-"),
    "axline":  lambda: plt.axline((1, 0.5), slope=0.5, color="green", linestyle="-."),
}

for name, build in cases.items():
    fig, ax = plt.subplots()
    ax.set_xlim(0, 4)
    ax.set_ylim(0, 2)
    build()

    p = tls.mpl_to_plotly(fig)  # 0 shapes before this fix
    print(f"{name:10s} -> {len(p.layout.shapes)} shape(s)")
    p.write_image(f"{name}_plotly.png")
axhline axline axvline
axhline_plotly axline_plotly axvline_plotly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants