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 843e517

Browse filesBrowse files
committed
GSOD: replace private lines dash info with LineStyle
1 parent ca4dfe0 commit 843e517
Copy full SHA for 843e517

File tree

4 files changed

+86
-100
lines changed
Filter options

4 files changed

+86
-100
lines changed

‎lib/matplotlib/_enums.py

Copy file name to clipboardExpand all lines: lib/matplotlib/_enums.py
+69-56Lines changed: 69 additions & 56 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, _AutoStringNameEnum):
229227
"""
230228
Describe if the line is solid or dashed, and the dash pattern, if any.
231229
@@ -278,25 +276,12 @@ class LineStyle(Enum):
278276
called when using the keyword *dashes* to the cycler , as shown in
279277
:doc:`property_cycle </tutorials/intermediate/color_cycle>`.
280278
"""
281-
solid = '-'
282-
dashed = '--'
283-
dotted = ':'
284-
dashdot = '-.'
285-
none = 'None'
286-
_custom = 'custom'
287-
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'}
279+
solid = auto()
280+
dashed = auto()
281+
dotted = auto()
282+
dashdot = auto()
283+
none = auto()
284+
custom = auto()
300285

301286
def __init__(self, ls, scale=1):
302287
"""
@@ -312,13 +297,14 @@ def __init__(self, ls, scale=1):
312297
factor.
313298
"""
314299

300+
self._linestyle_spec = ls
315301
if isinstance(ls, str):
316-
if ls in [' ', '', 'none']:
317-
ls = 'None'
318-
if ls in LineStyle.ls_mapper:
319-
ls = LineStyle.ls_mapper[ls]
302+
if ls in [' ', '', 'None']:
303+
ls = 'none'
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

Copy file name to clipboardExpand all lines: lib/matplotlib/lines.py
+6-18Lines changed: 6 additions & 18 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 ._enums import JoinStyle, CapStyle
20+
from ._enums import JoinStyle, CapStyle, LineStyle
2121

2222
# Imported here for backward compatibility, even though they don't
2323
# really belong.
@@ -269,7 +269,7 @@ def __init__(self, xdata, ydata,
269269
linewidth = rcParams['lines.linewidth']
270270

271271
if linestyle is None:
272-
linestyle = rcParams['lines.linestyle']
272+
linestyle = LineStyle(rcParams['lines.linestyle'])
273273
if marker is None:
274274
marker = rcParams['lines.marker']
275275
if markerfacecolor is None:
@@ -308,16 +308,8 @@ 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-
319-
self.set_linewidth(linewidth)
320311
self.set_linestyle(linestyle)
312+
self.set_linewidth(linewidth)
321313
self.set_drawstyle(drawstyle)
322314

323315
self._color = None
@@ -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

@@ -849,7 +841,7 @@ def get_linestyle(self):
849841
850842
See also `~.Line2D.set_linestyle`.
851843
"""
852-
return self._linestyle
844+
return self._linestyle._linestyle_spec
853845

854846
def get_linewidth(self):
855847
"""
@@ -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

‎lib/matplotlib/patches.py

Copy file name to clipboardExpand all lines: lib/matplotlib/patches.py
+10-26Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
get_parallels, inside_circle, make_wedged_bezier2,
1717
split_bezier_intersecting_with_closedpath, split_path_inout)
1818
from .path import Path
19-
from ._enums import JoinStyle, CapStyle
19+
from ._enums import JoinStyle, CapStyle, LineStyle
2020

2121

2222
@cbook._define_aliases({
@@ -92,8 +92,6 @@ def __init__(self,
9292
else:
9393
self.set_edgecolor(edgecolor)
9494
self.set_facecolor(facecolor)
95-
# unscaled dashes. Needed to scale dash patterns by lw
96-
self._us_dashes = None
9795
self._linewidth = 0
9896

9997
self.set_fill(fill)
@@ -254,9 +252,8 @@ def update_from(self, other):
254252
self._fill = other._fill
255253
self._hatch = other._hatch
256254
self._hatch_color = other._hatch_color
257-
# copy the unscaled dash pattern
258-
self._us_dashes = other._us_dashes
259-
self.set_linewidth(other._linewidth) # also sets dash properties
255+
self.set_linestyle(other._linestyle)
256+
self.set_linewidth(other._linewidth)
260257
self.set_transform(other.get_data_transform())
261258
# If the transform of other needs further initialization, then it will
262259
# be the case for this artist too.
@@ -308,7 +305,7 @@ def get_linewidth(self):
308305

309306
def get_linestyle(self):
310307
"""Return the linestyle."""
311-
return self._linestyle
308+
return self._linestyle._linestyle_spec
312309

313310
def set_antialiased(self, aa):
314311
"""
@@ -404,10 +401,6 @@ def set_linewidth(self, w):
404401
w = mpl.rcParams['axes.linewidth']
405402

406403
self._linewidth = float(w)
407-
# scale the dash pattern by the linewidth
408-
offset, ls = self._us_dashes
409-
self._dashoffset, self._dashes = mlines._scale_dashes(
410-
offset, ls, self._linewidth)
411404
self.stale = True
412405

413406
def set_linestyle(self, ls):
@@ -438,16 +431,7 @@ def set_linestyle(self, ls):
438431
ls : {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
439432
The line style.
440433
"""
441-
if ls is None:
442-
ls = "solid"
443-
if ls in [' ', '', 'none']:
444-
ls = 'None'
445-
self._linestyle = ls
446-
# get the unscaled dash pattern
447-
offset, ls = self._us_dashes = mlines._get_dash_pattern(ls)
448-
# scale the dash pattern by the linewidth
449-
self._dashoffset, self._dashes = mlines._scale_dashes(
450-
offset, ls, self._linewidth)
434+
self._linestyle = LineStyle(ls)
451435
self.stale = True
452436

453437
def set_fill(self, b):
@@ -560,10 +544,12 @@ def _bind_draw_path_function(self, renderer):
560544
gc.set_foreground(self._edgecolor, isRGBA=True)
561545

562546
lw = self._linewidth
563-
if self._edgecolor[3] == 0 or self._linestyle == 'None':
547+
if self._edgecolor[3] == 0 or self.get_linestyle() == 'None':
564548
lw = 0
565549
gc.set_linewidth(lw)
566-
gc.set_dashes(self._dashoffset, self._dashes)
550+
dash_offset, onoffseq = self._linestyle.get_dashes(lw)
551+
# Patch has traditionally ignored the dashoffset.
552+
gc.set_dashes(0, onoffseq)
567553
gc.set_capstyle(self._capstyle)
568554
gc.set_joinstyle(self._joinstyle)
569555

@@ -600,9 +586,7 @@ def draw(self, renderer):
600586
# docstring inherited
601587
if not self.get_visible():
602588
return
603-
# Patch has traditionally ignored the dashoffset.
604-
with cbook._setattr_cm(self, _dashoffset=0), \
605-
self._bind_draw_path_function(renderer) as draw_path:
589+
with self._bind_draw_path_function(renderer) as draw_path:
606590
path = self.get_path()
607591
transform = self.get_transform()
608592
tpath = transform.transform_path_non_affine(path)

‎lib/matplotlib/rcsetup.py

Copy file name to clipboardExpand all lines: lib/matplotlib/rcsetup.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ def _validate_linestyle(ls):
546546
LineStyle((0, ls))
547547
except ValueError:
548548
raise e
549+
return ls
549550

550551

551552
validate_fillstyle = ValidateInStrings(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.