Skip to content

Commit d69ecc2

Browse files
committed
Merge pull request #18 from tacaswell/doc_defaultdict
TST/Doc
2 parents 862a2d9 + 943ee29 commit d69ecc2

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

doc/source/index.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,45 @@ We can use `Cycler` instances to cycle over one or more ``kwarg`` to
245245
ax2.plot(x, x*(i+1), **sty)
246246
247247

248+
Persistent Cycles
249+
-----------------
250+
251+
It can be useful to associate a given label with a style via
252+
dictionary lookup and to dynamically generate that mapping. This
253+
can easily be accomplished using a `~collections.defaultdict`
254+
255+
.. ipython:: python
256+
257+
from cycler import cycler as cy
258+
from collections import defaultdict
259+
260+
cyl = cy('c', 'rgb') + cy('lw', range(1, 4))
261+
262+
To get a finite set of styles
263+
264+
.. ipython:: python
265+
266+
finite_cy_iter = iter(cyl)
267+
dd_finite = defaultdict(lambda : next(finite_cy_iter))
268+
269+
or repeating
270+
271+
.. ipython:: python
272+
273+
loop_cy_iter = cyl()
274+
dd_loop = defaultdict(lambda : next(loop_cy_iter))
275+
276+
This can be helpful when plotting complex data which has both a classification
277+
and a label ::
278+
279+
finite_cy_iter = iter(cyl)
280+
styles = defaultdict(lambda : next(finite_cy_iter))
281+
for group, label, data in DataSet:
282+
ax.plot(data, label=label, **styles[group])
283+
284+
which will result in every ``data`` with the same ``group`` being plotted with
285+
the same style.
286+
248287
Exceptions
249288
----------
250289

test_cycler.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def test_copying():
186186
i2 = ['r', 'g', 'b']
187187
# For more mutation fun!
188188
i3 = [['y', 'g'], ['b', 'k']]
189-
189+
190190
c1 = cycler('c', i1)
191191
c2 = cycler('lw', i2)
192192
c3 = cycler('foo', i3)
@@ -265,3 +265,17 @@ def test_eq():
265265
yield _eq_test_helper, a, c, False
266266
d = cycler(c='ymk')
267267
yield _eq_test_helper, b, d, False
268+
269+
270+
def test_cycler_exceptions():
271+
assert_raises(TypeError, cycler)
272+
assert_raises(TypeError, cycler, 'c', 'rgb', lw=range(3))
273+
assert_raises(TypeError, cycler, 'c')
274+
assert_raises(TypeError, cycler, 'c', 'rgb', 'lw', range(3))
275+
276+
277+
def test_starange_init():
278+
c = cycler('r', 'rgb')
279+
c2 = cycler('lw', range(3))
280+
cy = Cycler(list(c), list(c2), zip)
281+
assert_equal(cy, c + c2)

0 commit comments

Comments
 (0)