Skip to content

Commit 98ff4b4

Browse files
committed
Support odd-length dash patterns in Agg.
All vector backends already support passing an odd-length dash pattern, duplicating the on/off array (so that on the second repetition "on" becomes "off" and vice-versa). This is also explicitly supported by the svg spec for `stroke-dasharray` ("If an odd number of values is provided, then the list of values is repeated to yield an even number of values. Thus, 5,3,2 is equivalent to 5,3,2,5,3,2.") and less explicitly by the pdf and ps specs, and by cairo. So the backend needing support is Agg; iterating twice over the sequence is likely simplest.
1 parent 5698325 commit 98ff4b4

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/matplotlib/tests/test_lines.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,9 @@ def test_marker_as_markerstyle():
217217

218218
assert_array_equal(line2.get_marker().vertices, triangle1.vertices)
219219
assert_array_equal(line3.get_marker().vertices, triangle1.vertices)
220+
221+
222+
@check_figures_equal()
223+
def test_odd_dashes(fig_test, fig_ref):
224+
fig_test.add_subplot().plot([1, 2], dashes=[1, 2, 3])
225+
fig_ref.add_subplot().plot([1, 2], dashes=[1, 2, 3, 1, 2, 3])

src/py_converters.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ int convert_dashes(PyObject *dashobj, void *dashesp)
226226
PyObject *dash_offset_obj = NULL;
227227
double dash_offset = 0.0;
228228
PyObject *dashes_seq = NULL;
229-
Py_ssize_t nentries;
230229

231230
if (!PyArg_ParseTuple(dashobj, "OO:dashes", &dash_offset_obj, &dashes_seq)) {
232231
return 0;
@@ -256,18 +255,17 @@ int convert_dashes(PyObject *dashobj, void *dashesp)
256255
return 0;
257256
}
258257

259-
nentries = PySequence_Size(dashes_seq);
260-
if (nentries % 2 != 0) {
261-
PyErr_Format(PyExc_ValueError, "dashes sequence must have an even number of elements");
262-
return 0;
263-
}
258+
Py_ssize_t nentries = PySequence_Size(dashes_seq);
259+
// If the dashpattern has odd length, iterate through it twice (in
260+
// accordance with the pdf/ps/svg specs).
261+
Py_ssize_t dash_pattern_length = (nentries % 2) ? 2 * nentries : nentries;
264262

265-
for (Py_ssize_t i = 0; i < nentries; ++i) {
263+
for (Py_ssize_t i = 0; i < dash_pattern_length; ++i) {
266264
PyObject *item;
267265
double length;
268266
double skip;
269267

270-
item = PySequence_GetItem(dashes_seq, i);
268+
item = PySequence_GetItem(dashes_seq, i % nentries);
271269
if (item == NULL) {
272270
return 0;
273271
}
@@ -280,7 +278,7 @@ int convert_dashes(PyObject *dashobj, void *dashesp)
280278

281279
++i;
282280

283-
item = PySequence_GetItem(dashes_seq, i);
281+
item = PySequence_GetItem(dashes_seq, i % nentries);
284282
if (item == NULL) {
285283
return 0;
286284
}

0 commit comments

Comments
 (0)