Skip to content

Commit dbc7342

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 dbc7342

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-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: 5 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,15 @@ 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+
Py_ssize_t dash_pattern_length = (nentries % 2) ? 2 * nentries : nentries;
264260

265-
for (Py_ssize_t i = 0; i < nentries; ++i) {
261+
for (Py_ssize_t i = 0; i < dash_pattern_length; ++i) {
266262
PyObject *item;
267263
double length;
268264
double skip;
269265

270-
item = PySequence_GetItem(dashes_seq, i);
266+
item = PySequence_GetItem(dashes_seq, i % nentries);
271267
if (item == NULL) {
272268
return 0;
273269
}
@@ -280,7 +276,7 @@ int convert_dashes(PyObject *dashobj, void *dashesp)
280276

281277
++i;
282278

283-
item = PySequence_GetItem(dashes_seq, i);
279+
item = PySequence_GetItem(dashes_seq, i % nentries);
284280
if (item == NULL) {
285281
return 0;
286282
}

0 commit comments

Comments
 (0)