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

Skip to content

Commit f3185ce

Browse files
oscargusmeeseeksmachine
authored andcommitted
Backport PR #24026: Don't modify Axes property cycle in stackplot
1 parent cd448d7 commit f3185ce

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/matplotlib/stackplot.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
(https://stackoverflow.com/users/66549/doug)
77
"""
88

9+
import itertools
10+
911
import numpy as np
1012

1113
from matplotlib import _api
@@ -70,7 +72,9 @@ def stackplot(axes, x, *args,
7072

7173
labels = iter(labels)
7274
if colors is not None:
73-
axes.set_prop_cycle(color=colors)
75+
colors = itertools.cycle(colors)
76+
else:
77+
colors = (axes._get_lines.get_next_color() for _ in y)
7478

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

110114
# Color between x = 0 and the first array.
111-
color = axes._get_lines.get_next_color()
112115
coll = axes.fill_between(x, first_line, stack[0, :],
113-
facecolor=color, label=next(labels, None),
116+
facecolor=next(colors), label=next(labels, None),
114117
**kwargs)
115118
coll.sticky_edges.y[:] = [0]
116119
r = [coll]
117120

118121
# Color between array i-1 and array i
119122
for i in range(len(y) - 1):
120-
color = axes._get_lines.get_next_color()
121123
r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :],
122-
facecolor=color, label=next(labels, None),
124+
facecolor=next(colors),
125+
label=next(labels, None),
123126
**kwargs))
124127
return r

lib/matplotlib/tests/test_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,10 +2859,11 @@ def test_stackplot():
28592859
ax.set_xlim((0, 10))
28602860
ax.set_ylim((0, 70))
28612861

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

0 commit comments

Comments
 (0)