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 735fe29

Browse filesBrowse files
committed
Don't modify Axes property cycle in stackplot
This is not documented, and likely unwanted, and it breaks using cycle colours ('C0', 'C1', etc.) in the colour list. Fixes #24024
1 parent 5c45952 commit 735fe29
Copy full SHA for 735fe29

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
@@ -2851,10 +2851,11 @@ def test_stackplot():
28512851
ax.set_xlim((0, 10))
28522852
ax.set_ylim((0, 70))
28532853

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.