@@ -2480,7 +2480,7 @@ def broken_barh(self, xranges, yrange, **kwargs):
24802480
24812481 @_preprocess_data ()
24822482 def stem (self , * args , linefmt = None , markerfmt = None , basefmt = None , bottom = 0 ,
2483- label = None ):
2483+ label = None , use_line_collection = False ):
24842484 """
24852485 Create a stem plot.
24862486
@@ -2539,6 +2539,12 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
25392539 label : str, optional, default: None
25402540 The label to use for the stems in legends.
25412541
2542+ use_line_collection : bool, optional, default: False
2543+ If ``True``, store and plot the stem lines as a
2544+ ~`.collections.LineCollection` instead of individual lines. This
2545+ significantly increases performance, and will become the default
2546+ option in Matplotlib 3.3. If ``False``, defaults to old behaviour.
2547+
25422548
25432549 Returns
25442550 -------
@@ -2570,6 +2576,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
25702576 x = y
25712577 y = np .asarray (args [0 ], dtype = float )
25722578 args = args [1 :]
2579+ self ._process_unit_info (xdata = x , ydata = y )
2580+ x = self .convert_xunits (x )
2581+ y = self .convert_yunits (y )
25732582
25742583 # defaults for formats
25752584 if linefmt is None :
@@ -2618,24 +2627,40 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
26182627 else :
26192628 basestyle , basemarker , basecolor = _process_plot_format (basefmt )
26202629
2630+ # New behaviour in 3.1 is to use a LineCollection for the stemlines
2631+ if use_line_collection :
2632+ stemlines = []
2633+ for thisx , thisy in zip (x , y ):
2634+ stemlines .append (((thisx , bottom ), (thisx , thisy )))
2635+ stemlines = mcoll .LineCollection (stemlines , linestyles = linestyle ,
2636+ colors = linecolor ,
2637+ label = '_nolegend_' )
2638+ self .add_collection (stemlines )
2639+ # Old behaviour is to plot each of the lines individually
2640+ else :
2641+ warnings .warn (
2642+ 'In Matplotlib 3.3 individual lines on a stem plot will be '
2643+ 'added as a LineCollection instead of individual lines.\n '
2644+ 'This significantly improves the performance of a stem plot.\n '
2645+ 'To remove this warning and switch to the new behaviour, '
2646+ 'set the "use_line_collection" keyword argument to True.' )
2647+ stemlines = []
2648+ for thisx , thisy in zip (x , y ):
2649+ l , = self .plot ([thisx , thisx ], [bottom , thisy ],
2650+ color = linecolor , linestyle = linestyle ,
2651+ marker = linemarker , label = "_nolegend_" )
2652+ stemlines .append (l )
2653+
26212654 markerline , = self .plot (x , y , color = markercolor , linestyle = markerstyle ,
26222655 marker = markermarker , label = "_nolegend_" )
26232656
2624- stemlines = []
2625- for thisx , thisy in zip (x , y ):
2626- l , = self .plot ([thisx , thisx ], [bottom , thisy ],
2627- color = linecolor , linestyle = linestyle ,
2628- marker = linemarker , label = "_nolegend_" )
2629- stemlines .append (l )
2630-
26312657 baseline , = self .plot ([np .min (x ), np .max (x )], [bottom , bottom ],
26322658 color = basecolor , linestyle = basestyle ,
26332659 marker = basemarker , label = "_nolegend_" )
26342660
26352661 stem_container = StemContainer ((markerline , stemlines , baseline ),
26362662 label = label )
26372663 self .add_container (stem_container )
2638-
26392664 return stem_container
26402665
26412666 @_preprocess_data (replace_names = ["x" , "explode" , "labels" , "colors" ])
0 commit comments