Skip to content

Switch default of stem(use_line_collection=...) to True. #16474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/api/next_api_changes/behaviour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ of *y*, the second column of *x* against the second column of *y*, **and** the
first column of *x* against the third column of *y*. This now raises an error
instead.

`.Text.update_from` now copies usetex state from the source Text
`.Text.update_from` now copies usetex state from the source Text
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`~.Axes.stem` now defaults to ``use_line_collection=True``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This creates the stem plot as a `.LineCollection` rather than individual
`.Line2D` objects, greatly improving performance.
4 changes: 2 additions & 2 deletions examples/lines_bars_and_markers/stem_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
x = np.linspace(0.1, 2 * np.pi, 41)
y = np.exp(np.sin(x))

plt.stem(x, y, use_line_collection=True)
plt.stem(x, y)
plt.show()

#############################################################################
Expand All @@ -24,7 +24,7 @@
# control adapt the line objects returned by `~.pyplot`.

markerline, stemlines, baseline = plt.stem(
x, y, linefmt='grey', markerfmt='D', bottom=1.1, use_line_collection=True)
x, y, linefmt='grey', markerfmt='D', bottom=1.1)
markerline.set_markerfacecolor('none')
plt.show()

Expand Down
3 changes: 1 addition & 2 deletions examples/lines_bars_and_markers/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@
ax.set(title="Matplotlib release dates")

markerline, stemline, baseline = ax.stem(dates, levels,
linefmt="C3-", basefmt="k-",
use_line_collection=True)
linefmt="C3-", basefmt="k-")

plt.setp(markerline, mec="k", mfc="w", zorder=3)

Expand Down
18 changes: 6 additions & 12 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2652,7 +2652,7 @@ def broken_barh(self, xranges, yrange, **kwargs):

@_preprocess_data()
def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
label=None, use_line_collection=False):
label=None, use_line_collection=True):
"""
Create a stem plot.

Expand Down Expand Up @@ -2709,12 +2709,12 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
label : str, default: None
The label to use for the stems in legends.

use_line_collection : bool, default: False
use_line_collection : bool, default: True
If ``True``, store and plot the stem lines as a
`~.collections.LineCollection` instead of individual lines. This
significantly increases performance, and will become the default
option in Matplotlib 3.3. If ``False``, defaults to the old
behavior of using a list of `.Line2D` objects.
`~.collections.LineCollection` instead of individual lines, which
significantly increases performance. If ``False``, defaults to the
old behavior of using a list of `.Line2D` objects. This parameter
may be deprecated in the future.

Returns
-------
Expand Down Expand Up @@ -2802,12 +2802,6 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
self.add_collection(stemlines)
# Old behaviour is to plot each of the lines individually
else:
cbook._warn_external(
'In Matplotlib 3.3 individual lines on a stem plot will be '
'added as a LineCollection instead of individual lines. '
'This significantly improves the performance of a stem plot. '
'To remove this warning and switch to the new behaviour, '
'set the "use_line_collection" keyword argument to True.')
stemlines = []
for xi, yi in zip(x, y):
l, = self.plot([xi, xi], [bottom, yi],
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2735,7 +2735,7 @@ def stackplot(
@docstring.copy(Axes.stem)
def stem(
*args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
label=None, use_line_collection=False, data=None):
label=None, use_line_collection=True, data=None):
return gca().stem(
*args, linefmt=linefmt, markerfmt=markerfmt, basefmt=basefmt,
bottom=bottom, label=label,
Expand Down
29 changes: 6 additions & 23 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3341,28 +3341,11 @@ def test_stem(use_line_collection):
label=' ', use_line_collection=use_line_collection)

fig, ax = plt.subplots()
if use_line_collection:
ax.stem(*args, **kwargs)
else:
with pytest.warns(UserWarning):
ax.stem(*args, **kwargs)
ax.stem(*args, **kwargs)

ax.legend()


@check_figures_equal(extensions=['png'])
def test_stem_params(fig_test, fig_ref):
x = np.linspace(0, 3.14, 37)
y = np.sin(x)

ax = fig_test.subplots()
ax.stem(x, y, linefmt='grey', use_line_collection=True)

ax = fig_ref.subplots()
with pytest.warns(UserWarning):
ax.stem(x, y, linefmt='grey')


def test_stem_args():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
Expand All @@ -3371,18 +3354,18 @@ def test_stem_args():
y = list(range(10))

# Test the call signatures
ax.stem(y, use_line_collection=True)
ax.stem(x, y, use_line_collection=True)
ax.stem(x, y, 'r--', use_line_collection=True)
ax.stem(x, y, 'r--', basefmt='b--', use_line_collection=True)
ax.stem(y)
ax.stem(x, y)
ax.stem(x, y, 'r--')
ax.stem(x, y, 'r--', basefmt='b--')


def test_stem_dates():
fig, ax = plt.subplots(1, 1)
xs = [dateutil.parser.parse("2013-9-28 11:00:00"),
dateutil.parser.parse("2013-9-28 12:00:00")]
ys = [100, 200]
ax.stem(xs, ys, "*-", use_line_collection=True)
ax.stem(xs, ys, "*-")


@image_comparison(['hist_stacked_stepfilled_alpha'])
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def test_stem_remove():
ax = plt.gca()
st = ax.stem([1, 2], [1, 2], use_line_collection=True)
st = ax.stem([1, 2], [1, 2])
st.remove()


Expand Down