@@ -2625,7 +2625,7 @@ def broken_barh(self, xranges, yrange, **kwargs):
26252625
26262626 @_preprocess_data ()
26272627 def stem (self , * args , linefmt = None , markerfmt = None , basefmt = None , bottom = 0 ,
2628- label = None ):
2628+ label = None , use_line_collection = False ):
26292629 """
26302630 Create a stem plot.
26312631
@@ -2684,6 +2684,13 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
26842684 label : str, optional, default: None
26852685 The label to use for the stems in legends.
26862686
2687+ use_line_collection : bool, optional, default: False
2688+ If ``True``, store and plot the stem lines as a
2689+ `~.collections.LineCollection` instead of individual lines. This
2690+ significantly increases performance, and will become the default
2691+ option in Matplotlib 3.3. If ``False``, defaults to the old
2692+ behavior of using a list of `.Line2D` objects.
2693+
26872694
26882695 Returns
26892696 -------
@@ -2715,6 +2722,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
27152722 x = y
27162723 y = np .asarray (args [0 ], dtype = float )
27172724 args = args [1 :]
2725+ self ._process_unit_info (xdata = x , ydata = y )
2726+ x = self .convert_xunits (x )
2727+ y = self .convert_yunits (y )
27182728
27192729 # defaults for formats
27202730 if linefmt is None :
@@ -2763,24 +2773,39 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
27632773 else :
27642774 basestyle , basemarker , basecolor = _process_plot_format (basefmt )
27652775
2776+ # New behaviour in 3.1 is to use a LineCollection for the stemlines
2777+ if use_line_collection :
2778+ stemlines = []
2779+ stemlines = [((xi , bottom ), (xi , yi )) for xi , yi in zip (x , y )]
2780+ stemlines = mcoll .LineCollection (stemlines , linestyles = linestyle ,
2781+ colors = linecolor ,
2782+ label = '_nolegend_' )
2783+ self .add_collection (stemlines )
2784+ # Old behaviour is to plot each of the lines individually
2785+ else :
2786+ cbook ._warn_external (
2787+ 'In Matplotlib 3.3 individual lines on a stem plot will be '
2788+ 'added as a LineCollection instead of individual lines. '
2789+ 'This significantly improves the performance of a stem plot. '
2790+ 'To remove this warning and switch to the new behaviour, '
2791+ 'set the "use_line_collection" keyword argument to True.' )
2792+ stemlines = []
2793+ for xi , yi in zip (x , y ):
2794+ l , = self .plot ([xi , xi ], [bottom , yi ],
2795+ color = linecolor , linestyle = linestyle ,
2796+ marker = linemarker , label = "_nolegend_" )
2797+ stemlines .append (l )
2798+
27662799 markerline , = self .plot (x , y , color = markercolor , linestyle = markerstyle ,
27672800 marker = markermarker , label = "_nolegend_" )
27682801
2769- stemlines = []
2770- for thisx , thisy in zip (x , y ):
2771- l , = self .plot ([thisx , thisx ], [bottom , thisy ],
2772- color = linecolor , linestyle = linestyle ,
2773- marker = linemarker , label = "_nolegend_" )
2774- stemlines .append (l )
2775-
27762802 baseline , = self .plot ([np .min (x ), np .max (x )], [bottom , bottom ],
27772803 color = basecolor , linestyle = basestyle ,
27782804 marker = basemarker , label = "_nolegend_" )
27792805
27802806 stem_container = StemContainer ((markerline , stemlines , baseline ),
27812807 label = label )
27822808 self .add_container (stem_container )
2783-
27842809 return stem_container
27852810
27862811 @_preprocess_data (replace_names = ["x" , "explode" , "labels" , "colors" ])
0 commit comments