8000 ENH: enable stripey lines · matplotlib/matplotlib@9d4c639 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d4c639

Browse files
arndRemyrcomer
authored andcommitted
ENH: enable stripey lines
1 parent 028f07c commit 9d4c639

File tree

5 files changed

+84
-8
lines changed

5 files changed

+84
-8
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Stripey Lines
2+
-------------
3+
4+
New *offcolor* parameter enables the creation of stripey lines.
5+
6+
.. plot::
7+
:include-source: true
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
x = np.linspace(1., 3., 10)
13+
y = x**3
14+
15+
fig, ax = plt.subplots()
16+
ax.plot(x, y, linestyle='--', color='orange', offcolor='blue', linewidth=3,
17+
label='a striped line')
18+
ax.legend()
19+
plt.show()

examples/lines_bars_and_markers/line_demo_dash_control.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,33 @@
1717
:doc:`property_cycle </tutorials/intermediate/color_cycle>`
1818
by passing a list of dash sequences using the keyword *dashes* to the
1919
cycler. This is not shown within this example.
20+
21+
Other attributes of the dash may also be set either with the relevant method
22+
(`~.Line2D.set_dash_capstyle`, `~.Line2D.set_dash_joinstyle`,
23+
`~.Line2D.set_offcolor`) or by passing the property through a plotting
24+
function.
2025
"""
2126
import numpy as np
2227
import matplotlib.pyplot as plt
2328

2429
x = np.linspace(0, 10, 500)
2530
y = np.sin(x)
2631

32+
plt.rc('lines', linewidth=2.5)
2733
fig, ax = plt.subplots()
2834

29-
# Using set_dashes() to modify dashing of an existing line
30-
line1, = ax.plot(x, y, label='Using set_dashes()')
31-
line1.set_dashes([2, 2, 10, 2]) # 2pt line, 2pt break, 10pt line, 2pt break
35+
# Using set_dashes() and set_capstyle() to modify dashing of an existing line.
36+
line1, = ax.plot(x, y, label='Using set_dashes() and set_dash_capstyle()')
37+
line1.set_dashes([2, 2, 10, 2]) # 2pt line, 2pt break, 10pt line, 2pt break.
38+
line1.set_dash_capstyle('round')
3239

33-
# Using plot(..., dashes=...) to set the dashing when creating a line
40+
# Using plot(..., dashes=...) to set the dashing when creating a line.
3441
line2, = ax.plot(x, y - 0.2, dashes=[6, 2], label='Using the dashes parameter')
3542

36-
ax.legend()
43+
# Using plot(..., dashes=..., offcolor=...) to set the dashing and alternating
44+
# color when creating a line.
45+
line3, = ax.plot(x, y - 0.4, dashes=[4, 4], offcolor='tab:pink',
46+
label='Using the dashes and offcolor parameters')
47+
48+
ax.legend(handlelength=4)
3749
plt.show()

lib/matplotlib/lines.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def _slice_or_none(in_v, slc):
204204
@_api.define_aliases({
205205
"antialiased": ["aa"],
206206
"color": ["c"],
207+
"offcolor": ["oc"],
207208
"drawstyle": ["ds"],
208209
"linestyle": ["ls"],
209210
"linewidth": ["lw"],
@@ -272,6 +273,7 @@ def __init__(self, xdata, ydata,
272273
linewidth=None, # all Nones default to rc
273274
linestyle=None,
274275
color=None,
276+
offcolor=None,
275277
marker=None,
276278
markersize=None,
277279
markeredgewidth=None,
@@ -364,6 +366,9 @@ def __init__(self, xdata, ydata,
364366
else:
365367
self._marker = marker
366368

369+
self._offcolor = None
370+
self.set_offcolor(offcolor)
371+
367372
self._markevery = None
368373
self._markersize = None
369374
self._antialiased = None
@@ -754,9 +759,6 @@ def draw(self, renderer):
754759
self._set_gc_clip(gc)
755760
gc.set_url(self.get_url())
756761

757-
lc_rgba = mcolors.to_rgba(self._color, self._alpha)
758-
gc.set_foreground(lc_rgba, isRGBA=True)
759-
760762
gc.set_antialiased(self._antialiased)
761763
gc.set_linewidth(self._linewidth)
762764

@@ -772,6 +774,16 @@ def draw(self, renderer):
772774
if self.get_sketch_params() is not None:
773775
gc.set_sketch_params(*self.get_sketch_params())
774776

777+
# We first draw the path below if needed.
778+
if self.is_dashed() and self._offcolor is not None:
779+
lc_rgba = mcolors.to_rgba(self._offcolor, self._alpha)
780+
gc.set_foreground(lc_rgba, isRGBA=True)
781+
gc.set_dashes(0, None)
782+
renderer.draw_path(gc, tpath, affine.frozen())
783+
784+
lc_rgba = mcolors.to_rgba(self._color, self._alpha)
785+
gc.set_foreground(lc_rgba, isRGBA=True)
786+
775787
gc.set_dashes(*self._dash_pattern)
776788
renderer.draw_path(gc, tpath, affine.frozen())
777789
gc.restore()
@@ -868,6 +880,14 @@ def get_color(self):
868880
"""
869881
return self._color
870882

883+
def get_offcolor(self):
884+
"""
885+
Return the line offcolor.
886+
887+
See also `~.Line2D.set_offcolor`.
888+
"""
889+
return self._offcolor
890+
871891
def get_drawstyle(self):
872892
"""
873893
Return the drawstyle.
@@ -1031,6 +1051,20 @@ def set_color(self, color):
10311051
self._color = color
10321052
self.stale = True
10331053

1054+
def set_offcolor(self, offcolor):
1055+
"""
1056+
Set the gap color for the dashed line style.
1057+
1058+
Parameters
1059+
----------
1060+
offcolor : color or None
1061+
The gap color. If None, the gaps are unfilled.
1062+
"""
1063+
if offcolor is not None:
1064+
mcolors._check_color_like(color=offcolor)
1065+
self._offcolor = offcolor
1066+
self.stale = True
1067+
10341068
def set_drawstyle(self, drawstyle):
10351069
"""
10361070
Set the drawstyle of the plot.
@@ -1264,6 +1298,7 @@ def update_from(self, other):
12641298
self._linestyle = other._linestyle
12651299
self._linewidth = other._linewidth
12661300
self._color = other._color
1301+
self._offcolor = other._offcolor
12671302
self._markersize = other._markersize
12681303
self._markerfacecolor = other._markerfacecolor
12691304
self._markerfacecoloralt = other._markerfacecoloralt
Loading

Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ def test_marker_as_markerstyle():
304304
assert_array_equal(line3.get_marker().vertices, triangle1.vertices)
305305

306306

307+
@image_comparison(['striped_line.png'], remove_text=True, style='mpl20')
308+
def test_striped_lines():
309+
_, ax = plt.subplots()
310+
ax.plot([0, 1], color='orange', offcolor='blue', linestyle='--', lw=5,
311+
label='standard dash')
312+
ax.plot([0, 2], color='red', offcolor='black', linestyle=(0, (2, 5, 4, 2)),
313+
lw=5, label='custom dash')
314+
ax.legend(handlelength=5)
315+
316+
307317
@check_figures_equal()
308318
def test_odd_dashes(fig_test, fig_ref):
309319
fig_test.add_subplot().plot([1, 2], dashes=[1, 2, 3])