Skip to content

Commit 168c8b9

Browse files
authored
Merge pull request #21050 from anntzer/dashpat
Store dash_pattern as single attribute, not two.
2 parents 98115e9 + 9c0fa9e commit 168c8b9

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

lib/matplotlib/lines.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,8 @@ def __init__(self, xdata, ydata,
355355
self._linestyles = None
356356
self._drawstyle = None
357357
self._linewidth = linewidth
358-
359-
# scaled dash + offset
360-
self._dashSeq = None
361-
self._dashOffset = 0
362-
# unscaled dash + offset
363-
# this is needed scaling the dash pattern by linewidth
364-
self._us_dashSeq = None
365-
self._us_dashOffset = 0
358+
self._unscaled_dash_pattern = (0, None) # offset, dash
359+
self._dash_pattern = (0, None) # offset, dash (scaled by linewidth)
366360

367361
self.set_linewidth(linewidth)
368362
self.set_linestyle(linestyle)
@@ -779,7 +773,7 @@ def draw(self, renderer):
779773
if self.get_sketch_params() is not None:
780774
gc.set_sketch_params(*self.get_sketch_params())
781775

782-
gc.set_dashes(self._dashOffset, self._dashSeq)
776+
gc.set_dashes(*self._dash_pattern)
783777
renderer.draw_path(gc, tpath, affine.frozen())
784778
gc.restore()
785779

@@ -1083,13 +1077,10 @@ def set_linewidth(self, w):
10831077
Line width, in points.
10841078
"""
10851079
w = float(w)
1086-
10871080
if self._linewidth != w:
10881081
self.stale = True
10891082
self._linewidth = w
1090-
# rescale the dashes + offset
1091-
self._dashOffset, self._dashSeq = _scale_dashes(
1092-
self._us_dashOffset, self._us_dashSeq, self._linewidth)
1083+
self._dash_pattern = _scale_dashes(*self._unscaled_dash_pattern, w)
10931084

10941085
def set_linestyle(self, ls):
10951086
"""
@@ -1125,19 +1116,16 @@ def set_linestyle(self, ls):
11251116
if isinstance(ls, str):
11261117
if ls in [' ', '', 'none']:
11271118
ls = 'None'
1128-
11291119
_api.check_in_list([*self._lineStyles, *ls_mapper_r], ls=ls)
11301120
if ls not in self._lineStyles:
11311121
ls = ls_mapper_r[ls]
11321122
self._linestyle = ls
11331123
else:
11341124
self._linestyle = '--'
1135-
1136-
# get the unscaled dashes
1137-
self._us_dashOffset, self._us_dashSeq = _get_dash_pattern(ls)
1138-
# compute the linewidth scaled dashes
1139-
self._dashOffset, self._dashSeq = _scale_dashes(
1140-
self._us_dashOffset, self._us_dashSeq, self._linewidth)
1125+
self._unscaled_dash_pattern = _get_dash_pattern(ls)
1126+
self._dash_pattern = _scale_dashes(
1127+
*self._unscaled_dash_pattern, self._linewidth)
1128+
self.stale = True
11411129

11421130
@docstring.interpd
11431131
def set_marker(self, marker):
@@ -1282,10 +1270,8 @@ def update_from(self, other):
12821270
self._markerfacecoloralt = other._markerfacecoloralt
12831271
self._markeredgecolor = other._markeredgecolor
12841272
self._markeredgewidth = other._markeredgewidth
1285-
self._dashSeq = other._dashSeq
1286-
self._us_dashSeq = other._us_dashSeq
1287-
self._dashOffset = other._dashOffset
1288-
self._us_dashOffset = other._us_dashOffset
1273+
self._unscaled_dash_pattern = other._unscaled_dash_pattern
1274+
self._dash_pattern = other._dash_pattern
12891275
self._dashcapstyle = other._dashcapstyle
12901276
self._dashjoinstyle = other._dashjoinstyle
12911277
self._solidcapstyle = other._solidcapstyle

lib/matplotlib/patches.py

+14-18
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ def __init__(self,
9797
else:
9898
self.set_edgecolor(edgecolor)
9999
self.set_facecolor(facecolor)
100-
# unscaled dashes. Needed to scale dash patterns by lw
101-
self._us_dashes = None
100+
102101
self._linewidth = 0
102+
self._unscaled_dash_pattern = (0, None) # offset, dash
103+
self._dash_pattern = (0, None) # offset, dash (scaled by linewidth)
103104

104105
self.set_fill(fill)
105106
self.set_linestyle(linestyle)
@@ -259,9 +260,8 @@ def update_from(self, other):
259260
self._fill = other._fill
260261
self._hatch = other._hatch
261262
self._hatch_color = other._hatch_color
262-
# copy the unscaled dash pattern
263-
self._us_dashes = other._us_dashes
264-
self.set_linewidth(other._linewidth) # also sets dash properties
263+
self._unscaled_dash_pattern = other._unscaled_dash_pattern
264+
self.set_linewidth(other._linewidth) # also sets scaled dashes
265265
self.set_transform(other.get_data_transform())
266266
# If the transform of other needs further initialization, then it will
267267
# be the case for this artist too.
@@ -407,12 +407,9 @@ def set_linewidth(self, w):
407407
w = mpl.rcParams['patch.linewidth']
408408
if w is None:
409409
w = mpl.rcParams['axes.linewidth']
410-
411410
self._linewidth = float(w)
412-
# scale the dash pattern by the linewidth
413-
offset, ls = self._us_dashes
414-
self._dashoffset, self._dashes = mlines._scale_dashes(
415-
offset, ls, self._linewidth)
411+
self._dash_pattern = mlines._scale_dashes(
412+
*self._unscaled_dash_pattern, w)
416413
self.stale = True
417414

418415
def set_linestyle(self, ls):
@@ -445,11 +442,9 @@ def set_linestyle(self, ls):
445442
if ls in [' ', '', 'none']:
446443
ls = 'None'
447444
self._linestyle = ls
448-
# get the unscaled dash pattern
449-
offset, ls = self._us_dashes = mlines._get_dash_pattern(ls)
450-
# scale the dash pattern by the linewidth
451-
self._dashoffset, self._dashes = mlines._scale_dashes(
452-
offset, ls, self._linewidth)
445+
self._unscaled_dash_pattern = mlines._get_dash_pattern(ls)
446+
self._dash_pattern = mlines._scale_dashes(
447+
*self._unscaled_dash_pattern, self._linewidth)
453448
self.stale = True
454449

455450
def set_fill(self, b):
@@ -565,7 +560,7 @@ def _bind_draw_path_function(self, renderer):
565560
if self._edgecolor[3] == 0 or self._linestyle == 'None':
566561
lw = 0
567562
gc.set_linewidth(lw)
568-
gc.set_dashes(self._dashoffset, self._dashes)
563+
gc.set_dashes(*self._dash_pattern)
569564
gc.set_capstyle(self._capstyle)
570565
gc.set_joinstyle(self._joinstyle)
571566

@@ -603,8 +598,9 @@ def draw(self, renderer):
603598
if not self.get_visible():
604599
return
605600
# Patch has traditionally ignored the dashoffset.
606-
with cbook._setattr_cm(self, _dashoffset=0), \
607-
self._bind_draw_path_function(renderer) as draw_path:
601+
with cbook._setattr_cm(
602+
self, _dash_pattern=(0, self._dash_pattern[1])), \
603+
self._bind_draw_path_function(renderer) as draw_path:
608604
path = self.get_path()
609605
transform = self.get_transform()
610606
tpath = transform.transform_path_non_affine(path)

lib/matplotlib/tests/test_legend.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,7 @@ def test_linecollection_scaled_dashes():
481481
h1, h2, h3 = leg.legendHandles
482482

483483
for oh, lh in zip((lc1, lc2, lc3), (h1, h2, h3)):
484-
assert oh.get_linestyles()[0][1] == lh._dashSeq
485-
assert oh.get_linestyles()[0][0] == lh._dashOffset
484+
assert oh.get_linestyles()[0] == lh._dash_pattern
486485

487486

488487
def test_handler_numpoints():

0 commit comments

Comments
 (0)