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

Skip to content

Commit 5345d49

Browse files
committed
Deprecate support for dash-offset = None.
e.g. `plot([1, 2], ls=(None, (4, 4)))` which was previously a synonym for `plot([1, 2], ls=(0, (4, 4)))` which means "dash-pattern of 4pt-long dashes separated by 4pt spaces, with an offset of 0pt at start". Passing None instead of 0 was already not (likely, never) supported for pdf, ps, or svg (an exception is raised at savefig() time for ps/svg, an invalid pdf is generated). There isn't much of a point in supporting None (this also makes e.g. mplcairo more complex because of static typing in C++ extensions), so just deprecate it.
1 parent 664ab35 commit 5345d49

File tree

9 files changed

+46
-22
lines changed

9 files changed

+46
-22
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ PDF and PS character tracking internals
4747
The ``used_characters`` attribute and ``track_characters`` and
4848
``merge_used_characters`` methods of `.RendererPdf`, `.PdfFile`, and
4949
`.RendererPS` are deprecated.
50+
51+
Passing the dash offset as None
52+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53+
Fine control of dash patterns can be achieved by passing an
54+
``(offset, (on-length, off-length, on-length, off-length, ...))`` pair as the
55+
linestyle property of `.Line2D` and `.LineCollection`. Previously, certain
56+
APIs would accept ``offset = None`` as a synonym for ``offset = 0``, but this
57+
was never implemented e.g. for vector output. Support for ``offset = None`` is
58+
deprecated, set the offset to 0 instead.

examples/text_labels_and_annotations/legend_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def create_artists(self, legend, orig_handle,
153153
lw = orig_handle.get_linewidths()[i]
154154
except IndexError:
155155
lw = orig_handle.get_linewidths()[0]
156-
if dashes[0] is not None:
156+
if dashes[1] is not None:
157157
legline.set_dashes(dashes[1])
158158
legline.set_color(color)
159159
legline.set_transform(trans)

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ def __init__(self):
716716
self._capstyle = 'butt'
717717
self._cliprect = None
718718
self._clippath = None
719-
self._dashes = None, None
719+
self._dashes = 0, None
720720
self._joinstyle = 'round'
721721
self._linestyle = 'solid'
722722
self._linewidth = 1

lib/matplotlib/collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ def __init__(self,
115115
cm.ScalarMappable.__init__(self, norm, cmap)
116116
# list of un-scaled dash patterns
117117
# this is needed scaling the dash pattern by linewidth
118-
self._us_linestyles = [(None, None)]
118+
self._us_linestyles = [(0, None)]
119119
# list of dash patterns
120-
self._linestyles = [(None, None)]
120+
self._linestyles = [(0, None)]
121121
# list of unbroadcast/scaled linewidths
122122
self._us_lw = [0]
123123
self._linewidths = [0]

lib/matplotlib/lines.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,26 @@ def _get_dash_pattern(style):
3737
style = ls_mapper.get(style, style)
3838
# un-dashed styles
3939
if style in ['solid', 'None']:
40-
offset, dashes = None, None
40+
offset = 0
41+
dashes = None
4142
# dashed styles
4243
elif style in ['dashed', 'dashdot', 'dotted']:
4344
offset = 0
4445
dashes = tuple(rcParams['lines.{}_pattern'.format(style)])
4546
#
4647
elif isinstance(style, tuple):
4748
offset, dashes = style
49+
if offset is None:
50+
cbook.warn_deprecated(
51+
"3.3", message="Passing the dash offset as None is deprecated "
52+
"since %(since)s and support for it will be removed "
53+
"%(removal)s; pass it as zero instead.")
54+
offset = 0
4855
else:
4956
raise ValueError('Unrecognized linestyle: %s' % str(style))
5057

5158
# normalize offset to be positive and shorter than the dash cycle
52-
if dashes is not None and offset is not None:
59+
if dashes is not None:
5360
dsum = sum(dashes)
5461
if dsum:
5562
offset %= dsum
@@ -60,14 +67,9 @@ def _get_dash_pattern(style):
6067
def _scale_dashes(offset, dashes, lw):
6168
if not rcParams['lines.scale_dashes']:
6269
return offset, dashes
63-
64-
scaled_offset = scaled_dashes = None
65-
if offset is not None:
66-
scaled_offset = offset * lw
67-
if dashes is not None:
68-
scaled_dashes = [x * lw if x is not None else None
69-
for x in dashes]
70-
70+
scaled_offset = offset * lw
71+
scaled_dashes = ([x * lw if x is not None else None for x in dashes]
72+
if dashes is not None else None)
7173
return scaled_offset, scaled_dashes
7274

7375

lib/matplotlib/rcsetup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,18 @@ def _is_iterable_not_string_like(x):
571571
and _is_iterable_not_string_like(ls[1])
572572
and len(ls[1]) % 2 == 0
573573
and all(isinstance(elem, Number) for elem in ls[1])):
574+
if ls[0] is None:
575+
cbook.warn_deprecated(
576+
"3.3", message="Passing the dash offset as None is deprecated "
577+
"since %(since)s and support for it will be removed "
578+
"%(removal)s; pass it as zero instead.")
579+
ls = (0, ls[1])
574580
return ls
575581
# For backcompat: (on, off, on, off, ...); the offset is implicitly None.
576582
if (_is_iterable_not_string_like(ls)
577583
and len(ls) % 2 == 0
578584
and all(isinstance(elem, Number) for elem in ls)):
579-
return (None, ls)
585+
return (0, ls)
580586
raise ValueError(f"linestyle {ls!r} is not a valid on-off ink sequence.")
581587

582588

lib/matplotlib/tests/test_collections.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test__EventCollection__get_linestyle():
118118
check to make sure the default linestyle matches the input linestyle
119119
'''
120120
_, coll, _ = generate_EventCollection_plot()
121-
assert coll.get_linestyle() == [(None, None)]
121+
assert coll.get_linestyle() == [(0, None)]
122122

123123

124124
def test__EventCollection__get_color():
@@ -606,11 +606,11 @@ def test_lslw_bcast():
606606
col.set_linestyles(['-', '-'])
607607
col.set_linewidths([1, 2, 3])
608608

609-
assert col.get_linestyles() == [(None, None)] * 6
609+
assert col.get_linestyles() == [(0, None)] * 6
610610
assert col.get_linewidths() == [1, 2, 3] * 2
611611

612612
col.set_linestyles(['-', '-', '-'])
613-
assert col.get_linestyles() == [(None, None)] * 3
613+
assert col.get_linestyles() == [(0, None)] * 3
614614
assert (col.get_linewidths() == [1, 2, 3]).all()
615615

616616

lib/matplotlib/tests/test_rcparams.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,9 @@ def generate_validator_testcases(valid):
370370
('', ''), (' ', ' '),
371371
('None', 'none'), ('none', 'none'),
372372
('DoTtEd', 'dotted'), # case-insensitive
373-
('1, 3', (None, (1, 3))),
374-
([1.23, 456], (None, [1.23, 456.0])),
375-
([1, 2, 3, 4], (None, [1.0, 2.0, 3.0, 4.0])),
376-
((None, [1, 2]), (None, [1, 2])),
373+
('1, 3', (0, (1, 3))),
374+
([1.23, 456], (0, [1.23, 456.0])),
375+
([1, 2, 3, 4], (0, [1.0, 2.0, 3.0, 4.0])),
377376
((0, [1, 2]), (0, [1, 2])),
378377
((-1, [1, 2]), (-1, [1, 2])),
379378
),

src/py_converters.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ int convert_dashes(PyObject *dashobj, void *dashesp)
237237
if (PyErr_Occurred()) {
238238
return 0;
239239
}
240+
} else {
241+
if (PyErr_WarnEx(PyExc_FutureWarning,
242+
"Passing the dash offset as None is deprecated since "
243+
"Matplotlib 3.3 and will be removed in Matplotlib 3.5; "
244+
"pass it as zero instead.",
245+
1)) {
246+
return 0;
247+
}
240248
}
241249

242250
if (dashes_seq == Py_None) {

0 commit comments

Comments
 (0)