Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 069785d

Browse filesBrowse files
efiringtacaswell
authored andcommitted
Merge pull request #5926 from mdboom/new-dash-styles
Fix #5917. New dash patterns. Scale dashes by lw
1 parent bb6978c commit 069785d
Copy full SHA for 069785d

File tree

Expand file treeCollapse file tree

7 files changed

+55
-39
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+55
-39
lines changed

‎lib/matplotlib/backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+16-23Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.py
+3-9Lines changed: 3 additions & 9 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,16 @@ 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):
536536
ls = cbook.ls_mapper.get(ls, ls)
537-
if ls in dashd:
538-
dashes = [dashd[ls]]
539-
else:
540-
raise ValueError()
537+
dashes = [mlines.get_dash_pattern(ls)]
541538
elif cbook.iterable(ls):
542539
try:
543540
dashes = []
544541
for x in ls:
545542
if cbook.is_string_like(x):
546543
x = cbook.ls_mapper.get(x, x)
547-
if x in dashd:
548-
dashes.append(dashd[x])
549-
else:
550-
raise ValueError()
544+
dashes.append(mlines.get_dash_pattern(x))
551545
elif cbook.iterable(x) and len(x) == 2:
552546
dashes.append(x)
553547
else:

‎lib/matplotlib/lines.py

Copy file name to clipboardExpand all lines: lib/matplotlib/lines.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@
3737
from matplotlib import _path
3838

3939

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

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

Copy file name to clipboardExpand all lines: lib/matplotlib/mpl-data/stylelib/classic.mplstyle
+3Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/rcsetup.py
+9-6Lines 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

Copy file name to clipboardExpand all lines: matplotlibrc.template
+5Lines 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

Copy file name to clipboardExpand all lines: src/py_converters.cpp
+1-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.