Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #24026 on branch v3.6.x (Don't modify Axes property cycle in stackplot) #24029

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
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
13 changes: 8 additions & 5 deletions lib/matplotlib/stackplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
(https://stackoverflow.com/users/66549/doug)
"""

import itertools

import numpy as np

from matplotlib import _api
Expand Down Expand Up @@ -70,7 +72,9 @@ def stackplot(axes, x, *args,

labels = iter(labels)
if colors is not None:
axes.set_prop_cycle(color=colors)
colors = itertools.cycle(colors)
else:
colors = (axes._get_lines.get_next_color() for _ in y)

# Assume data passed has not been 'stacked', so stack it here.
# We'll need a float buffer for the upcoming calculations.
Expand Down Expand Up @@ -108,17 +112,16 @@ def stackplot(axes, x, *args,
stack += first_line

# Color between x = 0 and the first array.
color = axes._get_lines.get_next_color()
coll = axes.fill_between(x, first_line, stack[0, :],
facecolor=color, label=next(labels, None),
facecolor=next(colors), label=next(labels, None),
**kwargs)
coll.sticky_edges.y[:] = [0]
r = [coll]

# Color between array i-1 and array i
for i in range(len(y) - 1):
color = axes._get_lines.get_next_color()
r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :],
facecolor=color, label=next(labels, None),
facecolor=next(colors),
label=next(labels, None),
**kwargs))
return r
5 changes: 3 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2859,10 +2859,11 @@ def test_stackplot():
ax.set_xlim((0, 10))
ax.set_ylim((0, 70))

# Reuse testcase from above for a labeled data test
# Reuse testcase from above for a test with labeled data and with colours
# from the Axes property cycle.
data = {"x": x, "y1": y1, "y2": y2, "y3": y3}
fig, ax = plt.subplots()
ax.stackplot("x", "y1", "y2", "y3", data=data)
ax.stackplot("x", "y1", "y2", "y3", data=data, colors=["C0", "C1", "C2"])
ax.set_xlim((0, 10))
ax.set_ylim((0, 70))

Expand Down