Skip to content

Store dash_pattern as single attribute, not two. #21050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,8 @@ def __init__(self, xdata, ydata,
self._linestyles = None
self._drawstyle = None
self._linewidth = linewidth

# scaled dash + offset
self._dashSeq = None
self._dashOffset = 0
# unscaled dash + offset
# this is needed scaling the dash pattern by linewidth
self._us_dashSeq = None
self._us_dashOffset = 0
self._unscaled_dash_pattern = (0, None) # offset, dash
self._dash_pattern = (0, None) # offset, dash (scaled by linewidth)

self.set_linewidth(linewidth)
self.set_linestyle(linestyle)
Expand Down Expand Up @@ -775,7 +769,7 @@ def draw(self, renderer):
if self.get_sketch_params() is not None:
gc.set_sketch_params(*self.get_sketch_params())

gc.set_dashes(self._dashOffset, self._dashSeq)
gc.set_dashes(*self._dash_pattern)
renderer.draw_path(gc, tpath, affine.frozen())
gc.restore()

Expand Down Expand Up @@ -1079,13 +1073,10 @@ def set_linewidth(self, w):
Line width, in points.
"""
w = float(w)

if self._linewidth != w:
self.stale = True
self._linewidth = w
# rescale the dashes + offset
self._dashOffset, self._dashSeq = _scale_dashes(
self._us_dashOffset, self._us_dashSeq, self._linewidth)
self._dash_pattern = _scale_dashes(*self._unscaled_dash_pattern, w)

def set_linestyle(self, ls):
"""
Expand Down Expand Up @@ -1121,19 +1112,16 @@ def set_linestyle(self, ls):
if isinstance(ls, str):
if ls in [' ', '', 'none']:
ls = 'None'

_api.check_in_list([*self._lineStyles, *ls_mapper_r], ls=ls)
if ls not in self._lineStyles:
ls = ls_mapper_r[ls]
self._linestyle = ls
else:
self._linestyle = '--'

# get the unscaled dashes
self._us_dashOffset, self._us_dashSeq = _get_dash_pattern(ls)
# compute the linewidth scaled dashes
self._dashOffset, self._dashSeq = _scale_dashes(
self._us_dashOffset, self._us_dashSeq, self._linewidth)
self._unscaled_dash_pattern = _get_dash_pattern(ls)
self._dash_pattern = _scale_dashes(
*self._unscaled_dash_pattern, self._linewidth)
self.stale = True

@docstring.interpd
def set_marker(self, marker):
Expand Down Expand Up @@ -1278,10 +1266,8 @@ def update_from(self, other):
self._markerfacecoloralt = other._markerfacecoloralt
self._markeredgecolor = other._markeredgecolor
self._markeredgewidth = other._markeredgewidth
self._dashSeq = other._dashSeq
self._us_dashSeq = other._us_dashSeq
self._dashOffset = other._dashOffset
self._us_dashOffset = other._us_dashOffset
self._unscaled_dash_pattern = other._unscaled_dash_pattern
self._dash_pattern = other._dash_pattern
self._dashcapstyle = other._dashcapstyle
self._dashjoinstyle = other._dashjoinstyle
self._solidcapstyle = other._solidcapstyle
Expand Down
32 changes: 14 additions & 18 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ def __init__(self,
else:
self.set_edgecolor(edgecolor)
self.set_facecolor(facecolor)
# unscaled dashes. Needed to scale dash patterns by lw
self._us_dashes = None

self._linewidth = 0
self._unscaled_dash_pattern = (0, None) # offset, dash
self._dash_pattern = (0, None) # offset, dash (scaled by linewidth)

self.set_fill(fill)
self.set_linestyle(linestyle)
Expand Down Expand Up @@ -259,9 +260,8 @@ def update_from(self, other):
self._fill = other._fill
self._hatch = other._hatch
self._hatch_color = other._hatch_color
# copy the unscaled dash pattern
self._us_dashes = other._us_dashes
self.set_linewidth(other._linewidth) # also sets dash properties
self._unscaled_dash_pattern = other._unscaled_dash_pattern
self.set_linewidth(other._linewidth) # also sets scaled dashes
self.set_transform(other.get_data_transform())
# If the transform of other needs further initialization, then it will
# be the case for this artist too.
Expand Down Expand Up @@ -407,12 +407,9 @@ def set_linewidth(self, w):
w = mpl.rcParams['patch.linewidth']
if w is None:
w = mpl.rcParams['axes.linewidth']

self._linewidth = float(w)
# scale the dash pattern by the linewidth
offset, ls = self._us_dashes
self._dashoffset, self._dashes = mlines._scale_dashes(
offset, ls, self._linewidth)
self._dash_pattern = mlines._scale_dashes(
*self._unscaled_dash_pattern, w)
self.stale = True

def set_linestyle(self, ls):
Expand Down Expand Up @@ -445,11 +442,9 @@ def set_linestyle(self, ls):
if ls in [' ', '', 'none']:
ls = 'None'
self._linestyle = ls
# get the unscaled dash pattern
offset, ls = self._us_dashes = mlines._get_dash_pattern(ls)
# scale the dash pattern by the linewidth
self._dashoffset, self._dashes = mlines._scale_dashes(
offset, ls, self._linewidth)
self._unscaled_dash_pattern = mlines._get_dash_pattern(ls)
self._dash_pattern = mlines._scale_dashes(
*self._unscaled_dash_pattern, self._linewidth)
self.stale = True

def set_fill(self, b):
Expand Down Expand Up @@ -565,7 +560,7 @@ def _bind_draw_path_function(self, renderer):
if self._edgecolor[3] == 0 or self._linestyle == 'None':
lw = 0
gc.set_linewidth(lw)
gc.set_dashes(self._dashoffset, self._dashes)
gc.set_dashes(*self._dash_pattern)
gc.set_capstyle(self._capstyle)
gc.set_joinstyle(self._joinstyle)

Expand Down Expand Up @@ -603,8 +598,9 @@ def draw(self, renderer):
if not self.get_visible():
return
# Patch has traditionally ignored the dashoffset.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"backcompat".

with cbook._setattr_cm(self, _dashoffset=0), \
self._bind_draw_path_function(renderer) as draw_path:
with cbook._setattr_cm(
self, _dash_pattern=(0, self._dash_pattern[1])), \
self._bind_draw_path_function(renderer) as draw_path:
path = self.get_path()
transform = self.get_transform()
tpath = transform.transform_path_non_affine(path)
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ def test_linecollection_scaled_dashes():
h1, h2, h3 = leg.legendHandles

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


def test_handler_numpoints():
Expand Down