Skip to content

Commit 16ae357

Browse files
authored
Merge pull request #17444 from anntzer/odddash
Support odd-length dash patterns in Agg.
2 parents 0c0848b + 98ff4b4 commit 16ae357

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)