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

Skip to content

Commit 069b436

Browse files
arndRemyrcomer
authored andcommitted
ENH: enable stripey lines
1 parent 028f07c commit 069b436

File tree

5 files changed

+94
-8
lines changed

5 files changed

+94
-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: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def __init__(self, xdata, ydata,
272272
linewidth=None, # all Nones default to rc
273273
linestyle=None,
274274
color=None,
275+
offcolor=None,
275276
marker=None,
276277
markersize=None,
277278
markeredgewidth=None,
@@ -364,6 +365,9 @@ def __init__(self, xdata, ydata,
364365
else:
365366
self._marker = marker
366367

368+
self._offcolor = None
369+
self.set_offcolor(offcolor)
370+
367371
self._markevery = None
368372
self._markersize = None
369373
self._antialiased = None
@@ -754,9 +758,6 @@ def draw(self, renderer):
754758
self._set_gc_clip(gc)
755759
gc.set_url(self.get_url())
756760

757-
lc_rgba = mcolors.to_rgba(self._color, self._alpha)
758-
gc.set_foreground(lc_rgba, isRGBA=True)
759-
760761
gc.set_antialiased(self._antialiased)
761762
gc.set_linewidth(self._linewidth)
762763

@@ -772,6 +773,21 @@ def draw(self, renderer):
772773
if self.get_sketch_params() is not None:
773774
gc.set_sketch_params(*self.get_sketch_params())
774775

776+
# We first draw a path within the gaps if needed.
777+
if self.is_dashed() and self._offcolor is not None:
778+
lc_rgba = mcolors.to_rgba(self._offcolor, self._alpha)
779+
gc.set_foreground(lc_rgba, isRGBA=True)
780+
781+
dashes = self._dash_pattern[1]
782+
gaps = dashes[-1:] + dashes[:-1]
783+
offset_gaps = self._dash_pattern[0] + dashes[-1]
784+
785+
gc.set_dashes(offset_gaps, gaps)
786+
renderer.draw_path(gc, tpath, affine.frozen())
787+
788+
lc_rgba = mcolors.to_rgba(self._color, self._alpha)
789+
gc.set_foreground(lc_rgba, isRGBA=True)
790+
775791
gc.set_dashes(*self._dash_pattern)
776792
renderer.draw_path(gc, tpath, affine.frozen())
777793
gc.restore()
@@ -868,6 +884,14 @@ def get_color(self):
868884
"""
869885
return self._color
870886

887+
def get_offcolor(self):
888+
"""
889+
Return the line offcolor.
890+
891+
See also `~.Line2D.set_offcolor`.
892+
"""
893+
return self._offcolor
894+
871895
def get_drawstyle(self):
872896
"""
873897
Return the drawstyle.
@@ -1031,6 +1055,25 @@ def set_color(self, color):
10311055
self._color = color
10321056
self.stale = True
10331057

1058+
def set_offcolor(self, offcolor):
1059+
"""
1060+
Set the gap color for the dashed line style.
1061+
1062+
.. warning::
1063+
1064+
In some cases the two line colors may overlap, which could lead to
1065+
unwanted effects if also setting ``alpha``.
1066+
1067+
Parameters
1068+
----------
1069+
offcolor : color or None
1070+
The gap color. If None, the gaps are unfilled.
1071+
"""
1072+
if offcolor is not None:
1073+
mcolors._check_color_like(color=offcolor)
1074+
self._offcolor = offcolor
1075+
self.stale = True
1076+
10341077
def set_drawstyle(self, drawstyle):
10351078
"""
10361079
Set the drawstyle of the plot.
@@ -1264,6 +1307,7 @@ def update_from(self, other):
12641307
self._linestyle = other._linestyle
12651308
self._linewidth = other._linewidth
12661309
self._color = other._color
1310+
self._offcolor = other._offcolor
12671311
self._markersize = other._markersize
12681312
self._markerfacecolor = other._markerfacecolor
12691313
self._markerfacecoloralt = other._markerfacecoloralt
Loading

lib/matplotlib/tests/test_lines.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ 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+
rng = np.random.default_rng(19680801)
310+
_, ax = plt.subplots()
311+
ax.plot(rng.uniform(size=12), color='orange', offcolor='blue',
312+
linestyle='--', lw=5, label='standard dash')
313+
ax.plot(rng.uniform(size=12), color='red', offcolor='black',
314+
linestyle=(0, (2, 5, 4, 2)), lw=5, label='custom dash', alpha=0.5)
315+
ax.legend(handlelength=5)
316+
317+
307318
@check_figures_equal()
308319
def test_odd_dashes(fig_test, fig_ref):
309320
fig_test.add_subplot().plot([1, 2], dashes=[1, 2, 3])

0 commit comments

Comments
 (0)
0