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

Skip to content

Commit b73fe0f

Browse files
committed
Finished markevery validator and test cases. Added what's new and example code for markevery
1 parent 3bee936 commit b73fe0f

4 files changed

Lines changed: 121 additions & 18 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Implemented support for axes.prop_cycle property markevery in rcParams
2+
----------------------------------------------------------------------
3+
4+
The Matplotlib ``rcParams`` settings object now supports configuration
5+
of the attribute `axes.prop_cycle` with cyclers using the `markevery`
6+
Line2D object property. An example of this feature is provided at
7+
`~matplotlib/examples/lines_bars_and_markers/markevery_prop_cycle.py`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
=================================================================
3+
Implemented support for prop_cycle property markevery in rcParams
4+
=================================================================
5+
6+
This example demonstrates a working solution to issue #8576, providing full
7+
support of the markevery property for axes.prop_cycle assignments through
8+
rcParams. Makes use of the same list of markevery cases from
9+
https://matplotlib.org/examples/pylab_examples/markevery_demo.html
10+
11+
Renders a plot with shifted-sine curves along each column with
12+
a unique markevery value for each sine curve.
13+
"""
14+
from __future__ import division
15+
from cycler import cycler
16+
import numpy as np
17+
import matplotlib as mpl
18+
import matplotlib.pyplot as plt
19+
import matplotlib.patches as mpatches
20+
21+
# Define a list of markevery cases and color cases to plot
22+
cases = [None,
23+
8,
24+
(30, 8),
25+
[16, 24, 30],
26+
[0, -1],
27+
slice(100, 200, 3),
28+
0.1,
29+
0.3,
30+
1.5,
31+
(0.0, 0.1),
32+
(0.45, 0.1)]
33+
34+
colors = ['#1f77b4',
35+
'#ff7f0e',
36+
'#2ca02c',
37+
'#d62728',
38+
'#9467bd',
39+
'#8c564b',
40+
'#e377c2',
41+
'#7f7f7f',
42+
'#bcbd22',
43+
'#17becf',
44+
'#1a55FF']
45+
46+
# Create two different cyclers to use with axes.prop_cycle
47+
markevery_cycler = cycler(markevery=cases)
48+
color_cycler = cycler('color', colors)
49+
50+
# Configure rcParams axes.prop_cycle with custom cycler
51+
custom_cycler = color_cycler + markevery_cycler
52+
mpl.rcParams['axes.prop_cycle'] = custom_cycler
53+
54+
# Create data points and offsets
55+
x = np.linspace(0, 2 * np.pi)
56+
offsets = np.linspace(0, 2 * np.pi, 11, endpoint=False)
57+
yy = np.transpose([np.sin(x + phi) for phi in offsets])
58+
59+
# Set the plot curve with markers and a title
60+
fig = plt.figure()
61+
ax = fig.add_axes([0.1, 0.1, 0.6, 0.75])
62+
63+
for i in range(len(cases)):
64+
ax.plot(yy[:, i], marker='o', label=str(cases[i]))
65+
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
66+
67+
plt.title('Support for axes.prop_cycle cycler with markevery')
68+
69+
plt.show()

lib/matplotlib/rcsetup.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -541,27 +541,43 @@ def validate_markevery(s):
541541
Parameters
542542
----------
543543
s : None, int, float, slice, length-2 tuple of ints,
544-
length-2 tuple of floats, list
544+
length-2 tuple of floats, list of ints
545545
546546
Returns
547547
-------
548548
s : None, int, float, slice, length-2 tuple of ints,
549-
length-2 tuple of floats, list, ValueError
549+
length-2 tuple of floats, list of ints
550550
551551
"""
552+
# Validate s against type slice
553+
if isinstance(s, slice):
554+
return s
555+
# Validate s against type tuple and list
556+
if isinstance(s, Iterable):
557+
if isinstance(s, tuple):
558+
tupMaxLength = 2
559+
tupType = type(s[0])
560+
if len(s) != tupMaxLength:
561+
raise ValueError("'markevery' tuple must have a length "
562+
"of %d" % (tupMaxLength))
563+
if tupType is int and not all(isinstance(e, int) for e in s):
564+
raise ValueError("'markevery' tuple with first element of "
565+
"type int must have all elements of type "
566+
"int")
567+
if tupType is float and not all(isinstance(e, float) for e in s):
568+
raise ValueError("'markevery' tuple with first element of "
569+
"type float must have all elements of type "
570+
"float")
571+
if isinstance(s, list):
572+
if not all(isinstance(e, int) for e in s):
573+
raise ValueError("'markevery' list must have all elements "
574+
"of type int")
575+
# Validate s against type float int and None
576+
elif not isinstance(s, (float, int)):
577+
if s is not None:
578+
raise TypeError("'markevery' is of an invalid type")
552579

553-
if isinstance(s, tuple):
554-
# Ensure correct length of 2
555-
if len(s) != 2:
556-
raise ValueError("'markevery' tuple must be a length of 2")
557-
# Ensure that all elements in the tuple are of type int
558-
if not all(isinstance(x, int) for x in s):
559-
raise ValueError("'markevery' tuple ")
560-
# Ensure that all elements in the tuple are of type float
561-
elif not all(isinstance(x, float) for x in s):
562-
raise ValueError("'markevery' tuple ")
563-
564-
return s;
580+
return s
565581

566582
validate_markeverylist = _listify_validator(validate_markevery)
567583

lib/matplotlib/tests/test_rcparams.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,25 @@ def generate_validator_testcases(valid):
329329
)
330330
},
331331
{'validator': validate_markevery,
332-
'success': ((None,None),
333-
(1,1),
334-
(0.1,0.1)
332+
'success': ((None, None),
333+
(1, 1),
334+
(0.1, 0.1),
335+
((1,1), (1,1)),
336+
((0.1,0.1), (0.1,0.1)),
337+
([1,2,3], [1,2,3]),
338+
(slice(2), slice(None,2,None)),
339+
(slice(1,2,3), slice(1,2,3))
335340
),
336341
'fail': (((1,2,3), ValueError),
337342
((0.1,0.2,0.3), ValueError),
343+
((0.1,2,3), ValueError),
344+
((1,0.2,0.3), ValueError),
338345
((1,0.1), ValueError),
339-
(('abc'), ValueError)
346+
((0.1,1), ValueError),
347+
(('abc'), ValueError),
348+
(('a'), ValueError),
349+
('abc', ValueError),
350+
('a', ValueError)
340351
)
341352
}
342353
)

0 commit comments

Comments
 (0)