Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f3185ce

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

File tree

Expand file treeCollapse file tree

2 files changed

+11
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+11
-7
lines changed

‎lib/matplotlib/stackplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/stackplot.py
+8-5Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+3-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.