FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GSOD: replace private lines dash info with LineStyle · matplotlib/matplotlib@7b454c2 · GitHub

Repository navigation

Commit 7b454c2

Browse files
committed
GSOD: replace private lines dash info with LineStyle
1 parent b3499b2 commit 7b454c2

2 files changed

Lines changed: 64 additions & 63 deletions

File tree

‎lib/matplotlib/_types.py‎

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,20 @@ def demo():
210210
'CapStyle': CapStyle.input_description})
211211

212212

213-
def _get_dash_pattern(style):
214-
"""Convert linestyle to dash pattern."""
215-
# import must be local for validator code to live here
216-
from . import rcParams
217-
# un-dashed styles
218-
if style in ['solid', 'None']:
219-
offset = 0
220-
dashes = None
221-
# dashed styles
222-
elif style in ['dashed', 'dashdot', 'dotted']:
223-
offset = 0
224-
dashes = tuple(rcParams['lines.{}_pattern'.format(style)])
225-
return offset, dashes
226-
227-
228-
class LineStyle(Enum):
213+
#: Maps short codes for line style to their full name used by backends.
214+
_ls_mapper = {'-': 'solid', '--': 'dashed', '-.': 'dashdot', ':': 'dotted'}
215+
_deprecated_lineStyles = { # hidden names deprecated
216+
'-': '_draw_solid',
217+
'--': '_draw_dashed',
218+
'-.': '_draw_dash_dot',
219+
':': '_draw_dotted',
220+
'None': '_draw_nothing',
221+
' ': '_draw_nothing',
222+
'': '_draw_nothing',
223+
}
224+
225+
226+
class LineStyle(str, Enum):
229227
"""
230228
Describe if the line is solid or dashed, and the dash pattern, if any.
231229
@@ -285,19 +283,6 @@ class LineStyle(Enum):
285283
none = 'None'
286284
_custom = 'custom'
287285

288-
_deprecated_lineStyles = { # hidden names deprecated
289-
'-': '_draw_solid',
290-
'--': '_draw_dashed',
291-
'-.': '_draw_dash_dot',
292-
':': '_draw_dotted',
293-
'None': '_draw_nothing',
294-
' ': '_draw_nothing',
295-
'': '_draw_nothing',
296-
}
297-
298-
#: Maps short codes for line style to their full name used by backends.
299-
ls_mapper = {'-': 'solid', '--': 'dashed', '-.': 'dashdot', ':': 'dotted'}
300-
301286
def __init__(self, ls, scale=1):
302287
"""
303288
Parameters
@@ -312,13 +297,14 @@ def __init__(self, ls, scale=1):
312297
factor.
313298
"""
314299

300+
self._linestyle_spec = ls
315301
if isinstance(ls, str):
316302
if ls in [' ', '', 'none']:
317303
ls = 'None'
318-
if ls in LineStyle.ls_mapper:
319-
ls = LineStyle.ls_mapper[ls]
304+
if ls in _ls_mapper:
305+
ls = _ls_mapper[ls]
320306
Enum.__init__(self)
321-
offset, onoffseq = _get_dash_pattern(ls)
307+
offset, onoffseq = None, None
322308
else:
323309
try:
324310
offset, onoffseq = ls
@@ -342,22 +328,45 @@ def __init__(self, ls, scale=1):
342328
if not all(isinstance(elem, Number) for elem in onoffseq):
343329
raise ValueError('LineStyle onoffseq must be list of floats.')
344330
self._us_offset = offset
345-
self._us_onoffseq = dashes
346-
self.scale(scale)
347-
348-
@property
349-
def scale(self):
350-
return self._scale
351-
352-
@scale.setter
353-
def scale(self, s):
354-
if s < 0:
355-
raise ValueError('LineStyle cannot be scaled by a negative value.')
356-
self.offset = self._us_offset * s
357-
self.onoffseq = (
358-
[x * s if x is not None else None for x in self._us_onoffseq]
359-
if self._us_onoffseq is not None else None
360-
)
331+
self._us_onoffseq = onoffseq
332+
333+
def get_dashes(self, lw=1):
334+
"""
335+
Get the (scaled) dash sequence for this `.LineStyle`.
336+
"""
337+
# defer lookup until draw time
338+
if self._us_offset is None or self._us_onoffseq is None:
339+
self._us_offset, self._us_onoffseq = \
340+
LineStyle._get_dash_pattern(self.name)
341+
# normalize offset to be positive and shorter than the dash cycle
342+
dsum = sum(self._us_onoffseq)
343+
self._us_offset %= dsum
344+
return self._scale_dashes(self._us_offset, self._us_onoffseq, lw)
345+
346+
@staticmethod
347+
def _scale_dashes(offset, dashes, lw):
348+
from . import rcParams
349+
if not rcParams['lines.scale_dashes']:
350+
return offset, dashes
351+
scaled_offset = offset * lw
352+
scaled_dashes = ([x * lw if x is not None else None for x in dashes]
353+
if dashes is not None else None)
354+
return scaled_offset, scaled_dashes
355+
356+
@staticmethod
357+
def _get_dash_pattern(style):
358+
"""Convert linestyle string to explicit dash pattern."""
359+
# import must be local for validator code to live here
360+
from . import rcParams
361+
# un-dashed styles
362+
if style in ['solid', 'None']:
363+
offset = 0
364+
dashes = None
365+
# dashed styles
366+
elif style in ['dashed', 'dashdot', 'dotted']:
367+
offset = 0
368+
dashes = tuple(rcParams['lines.{}_pattern'.format(style)])
369+
return offset, dashes
361370

362371
@staticmethod
363372
def from_dashes(seq):
@@ -443,3 +452,7 @@ def plot_linestyles(ax, linestyles, title):
443452

444453
plt.tight_layout()
445454
plt.show()
455+
456+
457+
LineStyle._ls_mapper = _ls_mapper
458+
LineStyle._deprecated_lineStyles = _deprecated_lineStyles

‎lib/matplotlib/lines.py‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .path import Path
1818
from .transforms import (
1919
Affine2D, Bbox, BboxTransformFrom, BboxTransformTo, TransformedPath)
20-
from ._types import JoinStyle, CapStyle
20+
from ._types import JoinStyle, CapStyle, LineStyle
2121

2222
# Imported here for backward compatibility, even though they don't
2323
# really belong.
@@ -308,14 +308,6 @@ def __init__(self, xdata, ydata,
308308
self._drawstyle = None
309309
self._linewidth = linewidth
310310

311-
# scaled dash + offset
312-
self._dashSeq = None
313-
self._dashOffset = 0
314-
# unscaled dash + offset
315-
# this is needed scaling the dash pattern by linewidth
316-
self._us_dashSeq = None
317-
self._us_dashOffset = 0
318-
319311
self.set_linewidth(linewidth)
320312
self.set_linestyle(linestyle)
321313
self.set_drawstyle(drawstyle)
@@ -739,7 +731,7 @@ def draw(self, renderer):
739731
if self.get_sketch_params() is not None:
740732
gc.set_sketch_params(*self.get_sketch_params())
741733

742-
gc.set_dashes(self._dashOffset, self._dashSeq)
734+
gc.set_dashes(*self._linestyle.get_dashes(self._linewidth))
743735
renderer.draw_path(gc, tpath, affine.frozen())
744736
gc.restore()
745737

@@ -1191,7 +1183,7 @@ def set_ydata(self, y):
11911183
def set_dashes(self, seq):
11921184
self.set_linestyle(LineStyle.from_dashes(seq))
11931185

1194-
set_dashes.__doc__ = LineStyle.from_dashes.__doc_r
1186+
set_dashes.__doc__ = LineStyle.from_dashes.__doc__
11951187

11961188
def update_from(self, other):
11971189
"""Copy properties from *other* to self."""
@@ -1204,10 +1196,6 @@ def update_from(self, other):
12041196
self._markerfacecoloralt = other._markerfacecoloralt
12051197
self._markeredgecolor = other._markeredgecolor
12061198
self._markeredgewidth = other._markeredgewidth
1207-
self._dashSeq = other._dashSeq
1208-
self._us_dashSeq = other._us_dashSeq
1209-
self._dashOffset = other._dashOffset
1210-
self._us_dashOffset = other._us_dashOffset
12111199
self._dashcapstyle = other._dashcapstyle
12121200
self._dashjoinstyle = other._dashjoinstyle
12131201
self._solidcapstyle = other._solidcapstyle

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL