@@ -632,15 +632,35 @@ def draw_path_collection(self, **props):
632632
633633 def _draw_line_collection (self , props ):
634634 """Draw a path collection without face colors (e.g. contour lines)
635- as plain lines."""
635+ as plain lines, grouping consecutive same-style paths into single traces ."""
636636 edgecolors = mpltools .convert_rgba_array (props ["styles" ]["edgecolor" ])
637637 linewidths = mpltools .convert_linewidth_array (props ["styles" ]["linewidth" ])
638638 linestyles = props ["styles" ].get ("linestyle" )
639639
640+ current_style = None
641+ grouped_x = []
642+ grouped_y = []
643+
644+ def flush ():
645+ if current_style is not None and grouped_x :
646+ ec , lw , d = current_style
647+ self .plotly_fig .add_trace (
648+ go .Scatter (
649+ x = grouped_x ,
650+ y = grouped_y ,
651+ mode = "lines" ,
652+ showlegend = False ,
653+ line = go .scatter .Line (
654+ color = _export_color (ec ),
655+ width = lw ,
656+ dash = d ,
657+ ),
658+ xaxis = "x{0}" .format (self .axis_ct ),
659+ yaxis = "y{0}" .format (self .axis_ct ),
660+ )
661+ )
662+
640663 for i , (verts , codes ) in enumerate (props ["paths" ]):
641- edgecolor = _per_path (edgecolors , i , "rgba(0,0,0,0)" )
642- linewidth = _per_path (linewidths , i , 0 )
643- dash = _convert_collection_dash (_per_path (linestyles , i , None ))
644664 # a path may contain several disjoint lines (e.g. contour lines
645665 # of the same level); separate disjoint subpaths with None so
646666 # plotly does not connect them.
@@ -666,8 +686,8 @@ def _draw_line_collection(self, props):
666686 vi += step
667687 if current :
668688 subpaths .append ((current , closed ))
669- x_combined = []
670- y_combined = []
689+ path_x = []
690+ path_y = []
671691 for sub , closed in subpaths :
672692 if len (sub ) < 2 :
673693 continue
@@ -677,27 +697,31 @@ def _draw_line_collection(self, props):
677697 sub = sub + [sub [0 ]]
678698 sub_x = self ._convert_x_dates ([v [0 ] for v in sub ])
679699 sub_y = [v [1 ] for v in sub ]
680- if x_combined :
681- x_combined .append (None )
682- y_combined .append (None )
683- x_combined .extend (sub_x )
684- y_combined .extend (sub_y )
685- if x_combined :
686- self .plotly_fig .add_trace (
687- go .Scatter (
688- x = x_combined ,
689- y = y_combined ,
690- mode = "lines" ,
691- showlegend = False ,
692- line = go .scatter .Line (
693- color = _export_color (edgecolor ),
694- width = linewidth ,
695- dash = dash ,
696- ),
697- xaxis = "x{0}" .format (self .axis_ct ),
698- yaxis = "y{0}" .format (self .axis_ct ),
699- )
700- )
700+ if path_x :
701+ path_x .append (None )
702+ path_y .append (None )
703+ path_x .extend (sub_x )
704+ path_y .extend (sub_y )
705+ if not path_x :
706+ continue
707+
708+ edgecolor = _per_path (edgecolors , i , "rgba(0,0,0,0)" )
709+ linewidth = _per_path (linewidths , i , 0 )
710+ dash = _convert_collection_dash (_per_path (linestyles , i , None ))
711+ style = (edgecolor , linewidth , dash )
712+
713+ if style != current_style :
714+ flush ()
715+ current_style = style
716+ grouped_x = list (path_x )
717+ grouped_y = list (path_y )
718+ else :
719+ grouped_x .append (None )
720+ grouped_y .append (None )
721+ grouped_x .extend (path_x )
722+ grouped_y .extend (path_y )
723+
724+ flush ()
701725
702726 def _draw_filled_path_collection (self , props ):
703727 """Draw a path collection (e.g. violin plot bodies) as filled polygons."""
0 commit comments