99
1010import warnings
1111
12+ from matplotlib import transforms
1213import plotly .graph_objs as go
1314from plotly .matplotlylib .mplexporter import Renderer
1415from plotly .matplotlylib import mpltools
@@ -459,8 +460,18 @@ def draw_marked_line(self, **props):
459460 self .plotly_fig .add_trace (marked_line )
460461 self .msg += " Heck yeah, I drew that line\n "
461462 elif props ["coordinates" ] == "axes" :
462- # dealing with legend graphical elements
463- self .msg += " Using native legend\n "
463+ if self ._processing_legend :
464+ # dealing with legend graphical elements
465+ self .msg += " Using native legend\n "
466+ else :
467+ # horizontal/vertical reference lines (axhline/axvline)
468+ self ._draw_axes_line (props )
469+ elif props ["coordinates" ] == "display" and isinstance (
470+ props ["mplobj" ].get_transform (), transforms .BlendedGenericTransform
471+ ):
472+ # axhline/axvline: blended axes/data transforms are reported as
473+ # display coordinates by the exporter
474+ self ._draw_axes_line (props )
464475 else :
465476 self .msg += " Line didn't have 'data' coordinates, not drawing\n "
466477 warnings .warn (
@@ -469,6 +480,37 @@ def draw_marked_line(self, **props):
469480 "coordinates!"
470481 )
471482
483+ def _draw_axes_line (self , props ):
484+ """Draw an axes-coordinate reference line (axhline/axvline) as a
485+ layout shape spanning the line's endpoints in data coordinates."""
486+ ax = self .current_mpl_ax
487+ trans = props ["mplobj" ].get_transform ()
488+ if props ["coordinates" ] == "display" :
489+ px_points = props ["data" ]
490+ else :
491+ px_points = [trans .transform (pt ) for pt in props ["data" ]]
492+ (x0 , y0 ), (x1 , y1 ) = [ax .transData .inverted ().transform (pt ) for pt in px_points ]
493+ color = mpltools .merge_color_and_opacity (
494+ props ["linestyle" ]["color" ], props ["linestyle" ]["alpha" ]
495+ )
496+ shape = go .layout .Shape (
497+ type = "line" ,
498+ x0 = x0 ,
499+ y0 = y0 ,
500+ x1 = x1 ,
501+ y1 = y1 ,
502+ xref = "x{0}" .format (self .axis_ct ),
503+ yref = "y{0}" .format (self .axis_ct ),
504+ line = go .layout .shape .Line (
505+ color = color ,
506+ width = props ["linestyle" ]["linewidth" ],
507+ dash = mpltools .convert_dash (props ["linestyle" ]["dasharray" ]),
508+ ),
509+ layer = "above" ,
510+ )
511+ self .plotly_fig ["layout" ]["shapes" ] += (shape ,)
512+ self .msg += " Heck yeah, I drew that reference line\n "
513+
472514 def draw_image (self , ** props ):
473515 """Draw image.
474516
0 commit comments