@@ -2524,7 +2524,7 @@ def broken_barh(self, xranges, yrange, **kwargs):
25242524
25252525 @_preprocess_data (replace_all_args = True , label_namer = None )
25262526 def stem (self , * args , linefmt = None , markerfmt = None , basefmt = None ,
2527- bottom = 0 , label = None ):
2527+ bottom = 0 , label = None , use_line_collection = False ):
25282528 """
25292529 Create a stem plot.
25302530
@@ -2583,6 +2583,12 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None,
25832583 label : str, optional, default: None
25842584 The label to use for the stems in legends.
25852585
2586+ use_line_collection : bool, optional, default: False
2587+ If ``True``, store and plot the stem lines as a
2588+ ~`.collections.LineCollection` instead of individual lines. This
2589+ significantly increases performance, and will become the default
2590+ option in Matplotlib 3.3. If ``False``, defaults to old behaviour.
2591+
25862592
25872593 Returns
25882594 -------
@@ -2665,24 +2671,40 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None,
26652671 else :
26662672 basestyle , basemarker , basecolor = _process_plot_format (basefmt )
26672673
2674+ # New behaviour in 3.1 is to use a LineCollection for the stemlines
2675+ if use_line_collection :
2676+ stemlines = []
2677+ for thisx , thisy in zip (x , y ):
2678+ stemlines .append (((thisx , bottom ), (thisx , thisy )))
2679+ stemlines = mcoll .LineCollection (stemlines , linestyles = linestyle ,
2680+ colors = linecolor ,
2681+ label = '_nolegend_' )
2682+ self .add_collection (stemlines )
2683+ # Old behaviour is to plot each of the lines individually
2684+ else :
2685+ warnings .warn (
2686+ 'In Matplotlib 3.3 individual lines on a stem plot will be '
2687+ 'added as a LineCollection instead of individual lines.\n '
2688+ 'This significantly improves the performance of a stem plot.\n '
2689+ 'To remove this warning and switch to the new behaviour, '
2690+ 'set the "use_line_collection" keyword argument to True.' )
2691+ stemlines = []
2692+ for thisx , thisy in zip (x , y ):
2693+ l , = self .plot ([thisx , thisx ], [bottom , thisy ],
2694+ color = linecolor , linestyle = linestyle ,
2695+ marker = linemarker , label = "_nolegend_" )
2696+ stemlines .append (l )
2697+
26682698 markerline , = self .plot (x , y , color = markercolor , linestyle = markerstyle ,
26692699 marker = markermarker , label = "_nolegend_" )
26702700
2671- lines = []
2672- for thisx , thisy in zip (x , y ):
2673- lines .append (((thisx , bottom ), (thisx , thisy )))
2674- stemlines = mcoll .LineCollection (lines , linestyles = linestyle ,
2675- colors = linecolor , label = '_nolegend_' )
2676- self .add_collection (stemlines )
2677-
26782701 baseline , = self .plot ([np .min (x ), np .max (x )], [bottom , bottom ],
26792702 color = basecolor , linestyle = basestyle ,
26802703 marker = basemarker , label = "_nolegend_" )
26812704
26822705 stem_container = StemContainer ((markerline , stemlines , baseline ),
26832706 label = label )
26842707 self .add_container (stem_container )
2685-
26862708 return stem_container
26872709
26882710 @_preprocess_data (replace_names = ["x" , "explode" , "labels" , "colors" ],
0 commit comments