@@ -45,6 +45,21 @@ def _per_path(values, i, default):
4545 return values [i % n ] if n else default
4646
4747
48+ def _convert_collection_dash (linestyle ):
49+ """Convert a matplotlib collection line style to a plotly dash string.
50+
51+ Collections report line styles as (offset, dashes) tuples, with dashes
52+ in points and already scaled by line width (None for solid lines). Line
53+ widths are exported with their point values used as px, so the dashes
54+ are exported the same way, which keeps matplotlib's dash-to-width ratio.
55+ plotly has no dash offset, so the offset is dropped.
56+ """
57+ dashes = linestyle [1 ] if linestyle is not None else None
58+ if not dashes :
59+ return "solid"
60+ return "," .join ("{0:g}px" .format (d ) for d in dashes )
61+
62+
4863class PlotlyRenderer (Renderer ):
4964 """A renderer class inheriting from base for rendering mpl plots in plotly.
5065
@@ -526,6 +541,7 @@ def draw_path_collection(self, **props):
526541 'linewidth', (one or more linewidths)
527542 'facecolor', (one or more facecolors for path)
528543 'edgecolor', (one or more edgecolors for path)
544+ 'linestyle', (one or more (offset, dashes) line styles)
529545 'alpha', (one or more opacites for path)
530546 'zorder', (precedence when stacked)
531547 ]
@@ -565,10 +581,12 @@ def _draw_line_collection(self, props):
565581 as plain lines."""
566582 edgecolors = mpltools .convert_rgba_array (props ["styles" ]["edgecolor" ])
567583 linewidths = mpltools .convert_linewidth_array (props ["styles" ]["linewidth" ])
584+ linestyles = props ["styles" ].get ("linestyle" )
568585
569586 for i , (verts , codes ) in enumerate (props ["paths" ]):
570587 edgecolor = _per_path (edgecolors , i , "rgba(0,0,0,0)" )
571588 linewidth = _per_path (linewidths , i , 0 )
589+ dash = _convert_collection_dash (_per_path (linestyles , i , None ))
572590 # a path may contain several disjoint lines (e.g. contour lines
573591 # of the same level); separate disjoint subpaths with None so
574592 # plotly does not connect them.
@@ -617,7 +635,9 @@ def _draw_line_collection(self, props):
617635 y = y_combined ,
618636 mode = "lines" ,
619637 line = go .scatter .Line (
620- color = _export_color (edgecolor ), width = linewidth
638+ color = _export_color (edgecolor ),
639+ width = linewidth ,
640+ dash = dash ,
621641 ),
622642 xaxis = "x{0}" .format (self .axis_ct ),
623643 yaxis = "y{0}" .format (self .axis_ct ),
0 commit comments