Skip to content

Commit 1eed1a3

Browse files
committed
POC: make scaling optional in Collection.get_linestyle
1 parent a23ef5a commit 1eed1a3

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

lib/matplotlib/collections.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ def set_linestyle(self, ls):
626626
':', '', (offset, on-off-seq)}. See `.Line2D.set_linestyle` for a
627627
complete description.
628628
"""
629+
if isinstance(ls, (str, tuple)):
630+
self._original_linestyle = [ls]
631+
else:
632+
self._original_linestyle = ls
633+
629634
try:
630635
dashes = [mlines._get_dash_pattern(ls)]
631636
except ValueError:
@@ -866,8 +871,11 @@ def set_alpha(self, alpha):
866871
def get_linewidth(self):
867872
return self._linewidths
868873

869-
def get_linestyle(self):
870-
return self._linestyles
874+
def get_linestyle(self, scaled=False):
875+
if scaled:
876+
return self._linestyles
877+
878+
return self._original_linestyle
871879

872880
def _set_mappable_flags(self):
873881
"""

lib/matplotlib/collections.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Collection(colorizer.ColorizingArtist):
6868
def set_edgecolor(self, c: ColorType | Sequence[ColorType]) -> None: ...
6969
def set_alpha(self, alpha: float | Sequence[float] | None) -> None: ...
7070
def get_linewidth(self) -> float | Sequence[float]: ...
71-
def get_linestyle(self) -> LineStyleType | Sequence[LineStyleType]: ...
71+
def get_linestyle(self, scaled: bool = ...) -> LineStyleType | Sequence[LineStyleType]: ...
7272
def update_scalarmappable(self) -> None: ...
7373
def get_fill(self) -> bool: ...
7474
def update_from(self, other: Artist) -> None: ...

lib/matplotlib/legend_handler.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def get_numpoints(self, legend):
407407

408408
def _default_update_prop(self, legend_handle, orig_handle):
409409
lw = orig_handle.get_linewidths()[0]
410-
dashes = orig_handle._us_linestyles[0]
410+
dashes = orig_handle.get_linestyles()[0]
411411
color = orig_handle.get_colors()[0]
412412
legend_handle.set_color(color)
413413
legend_handle.set_linestyle(dashes)
@@ -798,7 +798,8 @@ def get_first(prop_array):
798798
legend_handle._hatch_color = orig_handle._hatch_color
799799
# Setters are fine for the remaining attributes.
800800
legend_handle.set_linewidth(get_first(orig_handle.get_linewidths()))
801-
legend_handle.set_linestyle(get_first(orig_handle.get_linestyles()))
801+
legend_handle.set_linestyle(
802+
get_first(orig_handle.get_linestyles(scaled=False)))
802803
legend_handle.set_transform(get_first(orig_handle.get_transforms()))
803804
legend_handle.set_figure(orig_handle.get_figure())
804805
# Alpha is already taken into account by the color attributes.

lib/matplotlib/tests/test_collections.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test__EventCollection__get_props():
8686
# check that the default lineoffset matches the input lineoffset
8787
assert props['lineoffset'] == coll.get_lineoffset()
8888
# check that the default linestyle matches the input linestyle
89-
assert coll.get_linestyle() == [(0, None)]
89+
assert coll.get_linestyle(scaled=True) == [(0, None)]
9090
# check that the default color matches the input color
9191
for color in [coll.get_color(), *coll.get_colors()]:
9292
np.testing.assert_array_equal(color, props['color'])
@@ -248,7 +248,7 @@ def test__EventCollection__set_lineoffset():
248248
])
249249
def test__EventCollection__set_prop():
250250
for prop, value, expected in [
251-
('linestyle', 'dashed', [(0, (6.0, 6.0))]),
251+
('linestyle', 'dashed', ['dashed']),
252252
('linestyle', (0, (6., 6.)), [(0, (6.0, 6.0))]),
253253
('linewidth', 5, 5),
254254
]:
@@ -666,11 +666,11 @@ def test_lslw_bcast():
666666
col.set_linestyles(['-', '-'])
667667
col.set_linewidths([1, 2, 3])
668668

669-
assert col.get_linestyles() == [(0, None)] * 6
669+
assert col.get_linestyles(scaled=True) == [(0, None)] * 6
670670
assert col.get_linewidths() == [1, 2, 3] * 2
671671

672672
col.set_linestyles(['-', '-', '-'])
673-
assert col.get_linestyles() == [(0, None)] * 3
673+
assert col.get_linestyles(scaled=True) == [(0, None)] * 3
674674
assert (col.get_linewidths() == [1, 2, 3]).all()
675675

676676

lib/matplotlib/tests/test_legend.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,16 @@ def test_legend_stackplot():
528528
ax.legend(loc='best')
529529

530530

531+
@mpl.style.context('default')
532+
def test_polycollection_linestyles_unscaled():
533+
fig, ax = plt.subplots()
534+
q = ax.quiver(0, 0, 7, 1, label='v1 + v2', linewidth=3, linestyle='dotted')
535+
leg = ax.legend()
536+
handle, = leg.legend_handles
537+
538+
assert q.get_linestyle(scaled=False)[0] == handle.get_linestyle()
539+
540+
531541
def test_cross_figure_patch_legend():
532542
fig, ax = plt.subplots()
533543
fig2, ax2 = plt.subplots()
@@ -612,7 +622,7 @@ def test_linecollection_scaled_dashes():
612622
h1, h2, h3 = leg.legend_handles
613623

614624
for oh, lh in zip((lc1, lc2, lc3), (h1, h2, h3)):
615-
assert oh.get_linestyles()[0] == lh._dash_pattern
625+
assert oh.get_linestyles()[0] == lh.get_linestyle()
616626

617627

618628
def test_handler_numpoints():

lib/mpl_toolkits/mplot3d/tests/test_legend3d.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_linecollection_scaled_dashes():
5555
h1, h2, h3 = leg.legend_handles
5656

5757
for oh, lh in zip((lc1, lc2, lc3), (h1, h2, h3)):
58-
assert oh.get_linestyles()[0] == lh._dash_pattern
58+
assert oh.get_linestyles()[0] == lh.get_linestyle()
5959

6060

6161
def test_handlerline3d():

0 commit comments

Comments
 (0)