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

Skip to content

Commit 6258dad

Browse files
committed
Merge pull request #5536 from WeatherGod/fix_cycle_reset
Fix the resetting of color cycles
2 parents 8fcbf60 + eed693f commit 6258dad

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,13 @@ def set_color_cycle(self, clist):
11231123
"""
11241124
cbook.warn_deprecated(
11251125
'1.5', name='set_color_cycle', alternative='set_prop_cycle')
1126-
self.set_prop_cycle('color', clist)
1126+
if clist is None:
1127+
# Calling set_color_cycle() or set_prop_cycle() with None
1128+
# effectively resets the cycle, but you can't do
1129+
# set_prop_cycle('color', None). So we are special-casing this.
1130+
self.set_prop_cycle(None)
1131+
else:
1132+
self.set_prop_cycle('color', clist)
11271133

11281134
def ishold(self):
11291135
"""return the HOLD status of the axes"""

lib/matplotlib/tests/test_cycles.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from matplotlib.testing.decorators import image_comparison, cleanup
24
import matplotlib.pyplot as plt
35
import numpy as np
@@ -147,6 +149,30 @@ def test_valid_input_forms():
147149
assert True
148150

149151

152+
@cleanup
153+
def test_cycle_reset():
154+
fig, ax = plt.subplots()
155+
156+
# Can't really test a reset because only a cycle object is stored
157+
# but we can test the first item of the cycle.
158+
prop = next(ax._get_lines.prop_cycler)
159+
ax.set_prop_cycle(linewidth=[10, 9, 4])
160+
assert prop != next(ax._get_lines.prop_cycler)
161+
ax.set_prop_cycle(None)
162+
got = next(ax._get_lines.prop_cycler)
163+
assert prop == got, "expected %s, got %s" % (prop, got)
164+
165+
fig, ax = plt.subplots()
166+
# Need to double-check the old set/get_color_cycle(), too
167+
with warnings.catch_warnings():
168+
prop = next(ax._get_lines.prop_cycler)
169+
ax.set_color_cycle(['c', 'm', 'y', 'k'])
170+
assert prop != next(ax._get_lines.prop_cycler)
171+
ax.set_color_cycle(None)
172+
got = next(ax._get_lines.prop_cycler)
173+
assert prop == got, "expected %s, got %s" % (prop, got)
174+
175+
150176
@cleanup
151177
def test_invalid_input_forms():
152178
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)