Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb52bc0

Browse files
committed
Fix #5917. New dash patterns. Scale dashes by lw
1 parent 2a4863c commit cb52bc0

File tree

7 files changed

+55
-40
lines changed

7 files changed

+55
-40
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from matplotlib import is_interactive
5656
from matplotlib import get_backend
5757
from matplotlib._pylab_helpers import Gcf
58+
from matplotlib import lines
5859

5960
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
6061

@@ -771,14 +772,6 @@ class GraphicsContextBase(object):
771772
An abstract base class that provides color, line styles, etc...
772773
"""
773774

774-
# a mapping from dash styles to suggested offset, dash pairs
775-
dashd = {
776-
'solid': (None, None),
777-
'dashed': (0, (6.0, 6.0)),
778-
'dashdot': (0, (3.0, 5.0, 1.0, 5.0)),
779-
'dotted': (0, (1.0, 3.0)),
780-
}
781-
782775
def __init__(self):
783776
self._alpha = 1.0
784777
self._forced_alpha = False # if True, _alpha overrides A from RGBA
@@ -870,7 +863,16 @@ def get_dashes(self):
870863
871864
Default value is None
872865
"""
873-
return self._dashes
866+
if rcParams['_internal.classic_mode']:
867+
return self._dashes
868+
else:
869+
scale = max(1.0, self.get_linewidth())
870+
offset, dashes = self._dashes
871+
if offset is not None:
872+
offset = offset * scale
873+
if dashes is not None:
874+
dashes = [x * scale for x in dashes]
875+
return offset, dashes
874876

875877
def get_forced_alpha(self):
876878
"""
@@ -1047,21 +1049,12 @@ def set_linewidth(self, w):
10471049
def set_linestyle(self, style):
10481050
"""
10491051
Set the linestyle to be one of ('solid', 'dashed', 'dashdot',
1050-
'dotted'). One may specify customized dash styles by providing
1051-
a tuple of (offset, dash pairs). For example, the predefiend
1052-
linestyles have following values.:
1053-
1054-
'dashed' : (0, (6.0, 6.0)),
1055-
'dashdot' : (0, (3.0, 5.0, 1.0, 5.0)),
1056-
'dotted' : (0, (1.0, 3.0)),
1052+
'dotted'). These are defined in the rcParams
1053+
`lines.dashed_pattern`, `lines.dashdot_pattern` and
1054+
`lines.dotted_pattern`. One may also specify customized dash
1055+
styles by providing a tuple of (offset, dash pairs).
10571056
"""
1058-
1059-
if style in self.dashd:
1060-
offset, dashes = self.dashd[style]
1061-
elif isinstance(style, tuple):
1062-
offset, dashes = style
1063-
else:
1064-
raise ValueError('Unrecognized linestyle: %s' % str(style))
1057+
offset, dashes = lines.get_dash_pattern(style)
10651058

10661059
self._linestyle = style
10671060
self.set_dashes(offset, dashes)

lib/matplotlib/collections.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import matplotlib.path as mpath
2929
from matplotlib import _path
3030
import matplotlib.mlab as mlab
31+
import matplotlib.lines as mlines
3132

3233

3334
CIRCLE_AREA_FACTOR = 1.0 / np.sqrt(np.pi)
@@ -531,23 +532,15 @@ def set_linestyle(self, ls):
531532
The line style.
532533
"""
533534
try:
534-
dashd = backend_bases.GraphicsContextBase.dashd
535535
if cbook.is_string_like(ls):
536-
ls = cbook.ls_mapper.get(ls, ls)
537-
if ls in dashd:
538-
dashes = [dashd[ls]]
539-
else:
540-
raise ValueError()
536+
dashes = [mlines.get_dash_pattern(ls)]
541537
elif cbook.iterable(ls):
542538
try:
543539
dashes = []
544540
for x in ls:
545541
if cbook.is_string_like(x):
546542
x = cbook.ls_mapper.get(x, x)
547-
if x in dashd:
548-
dashes.append(dashd[x])
549-
else:
550-
raise ValueError()
543+
dashes.append(mlines.get_dash_pattern(x))
551544
elif cbook.iterable(x) and len(x) == 2:
552545
dashes.append(x)
553546
else:

lib/matplotlib/lines.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@
3636
from matplotlib import _path
3737

3838

39+
def get_dash_pattern(style):
40+
"""
41+
Given a dash pattern name from 'solid', 'dashed', 'dashdot' or
42+
'dotted', returns the (offset, dashes) pattern.
43+
"""
44+
if style == 'solid':
45+
offset, dashes = None, None
46+
elif style in ['dashed', 'dashdot', 'dotted']:
47+
offset = 0
48+
dashes = tuple(rcParams['lines.{}_pattern'.format(style)])
49+
elif isinstance(style, tuple):
50+
offset, dashes = style
51+
else:
52+
raise ValueError('Unrecognized linestyle: %s' % str(style))
53+
54+
return offset, dashes
55+
56+
3957
def segment_hits(cx, cy, x, y, radius):
4058
"""
4159
Determine if any line segments are within radius of a

lib/matplotlib/mpl-data/stylelib/classic.mplstyle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ lines.dash_capstyle : butt # butt|round|projecting
1515
lines.solid_joinstyle : round # miter|round|bevel
1616
lines.solid_capstyle : projecting # butt|round|projecting
1717
lines.antialiased : True # render lines in antialiased (no jaggies)
18+
lines.dashed_pattern : 6, 6
19+
lines.dashdot_pattern : 3, 5, 1, 5
20+
lines.dotted_pattern : 1, 3
1821

1922
### Marker props
2023
markers.fillstyle: full

lib/matplotlib/rcsetup.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,18 @@ def validate_maskedarray(v):
275275

276276

277277
class validate_nseq_float(object):
278-
def __init__(self, n):
278+
def __init__(self, n=None):
279279
self.n = n
280280

281281
def __call__(self, s):
282282
"""return a seq of n floats or raise"""
283283
if isinstance(s, six.string_types):
284-
s = s.split(',')
284+
s = [x.strip() for x in s.split(',')]
285285
err_msg = _str_err_msg
286286
else:
287287
err_msg = _seq_err_msg
288288

289-
if len(s) != self.n:
289+
if self.n is not None and len(s) != self.n:
290290
raise ValueError(err_msg.format(n=self.n, num=len(s), s=s))
291291

292292
try:
@@ -296,18 +296,18 @@ def __call__(self, s):
296296

297297

298298
class validate_nseq_int(object):
299-
def __init__(self, n):
299+
def __init__(self, n=None):
300300
self.n = n
301301

302302
def __call__(self, s):
303303
"""return a seq of n ints or raise"""
304304
if isinstance(s, six.string_types):
305-
s = s.split(',')
305+
s = [x.strip() for x in s.split(',')]
306306
err_msg = _str_err_msg
307307
else:
308308
err_msg = _seq_err_msg
309309

310-
if len(s) != self.n:
310+
if self.n is not None and len(s) != self.n:
311311
raise ValueError(err_msg.format(n=self.n, num=len(s), s=s))
312312

313313
try:
@@ -836,6 +836,9 @@ def validate_hist_bins(s):
836836
'lines.solid_joinstyle': ['round', validate_joinstyle],
837837
'lines.dash_capstyle': ['butt', validate_capstyle],
838838
'lines.solid_capstyle': ['projecting', validate_capstyle],
839+
'lines.dashed_pattern': [[2.8, 1.2], validate_nseq_float()],
840+
'lines.dashdot_pattern': [[4.8, 1.2, 0.8, 1.2], validate_nseq_float()],
841+
'lines.dotted_pattern': [[1.2, 0.6], validate_nseq_float()],
839842

840843
# marker props
841844
'markers.fillstyle': ['full', validate_fillstyle],

matplotlibrc.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ backend : $TEMPLATE_BACKEND
9090
#lines.solid_capstyle : projecting # butt|round|projecting
9191
#lines.antialiased : True # render lines in antialiased (no jaggies)
9292

93+
# The three standard dash patterns. These are scaled by the linewidth.
94+
#lines.dashed_pattern : 2.8, 1.2
95+
#lines.dashdot_pattern : 4.8, 1.2, 0.8, 1.2
96+
#lines.dotted_pattern : 1.2, 0.6
97+
9398
#markers.fillstyle: full # full|left|right|bottom|top|none
9499

95100
### PATCHES

src/py_converters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ int convert_gcagg(PyObject *pygc, void *gcp)
476476
convert_from_attr(pygc, "_antialiased", &convert_bool, &gc->isaa) &&
477477
convert_from_attr(pygc, "_capstyle", &convert_cap, &gc->cap) &&
478478
convert_from_attr(pygc, "_joinstyle", &convert_join, &gc->join) &&
479-
convert_from_attr(pygc, "_dashes", &convert_dashes, &gc->dashes) &&
479+
convert_from_method(pygc, "get_dashes", &convert_dashes, &gc->dashes) &&
480480
convert_from_attr(pygc, "_cliprect", &convert_rect, &gc->cliprect) &&
481481
convert_from_method(pygc, "get_clip_path", &convert_clippath, &gc->clippath) &&
482482
convert_from_method(pygc, "get_snap", &convert_snap, &gc->snap_mode) &&

0 commit comments

Comments
 (0)