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 8247e61

Browse filesBrowse files
committed
Allow empty linestyle for collections
1 parent a861b8a commit 8247e61
Copy full SHA for 8247e61

15 files changed

+252
-133
lines changed

‎ci/mypy-stubtest-allowlist.txt

Copy file name to clipboardExpand all lines: ci/mypy-stubtest-allowlist.txt
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ matplotlib.ticker.LogLocator.set_params
8181
matplotlib.axes._base._AxesBase.axis
8282

8383
# Aliases (dynamically generated, not type hinted)
84-
matplotlib.collections.Collection.get_dashes
8584
matplotlib.collections.Collection.get_ec
8685
matplotlib.collections.Collection.get_edgecolors
8786
matplotlib.collections.Collection.get_facecolors

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8055,7 +8055,7 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
80558055
if 'linestyle' in kwargs:
80568056
raise _api.kwarg_error("spy", "linestyle")
80578057
ret = mlines.Line2D(
8058-
x, y, linestyle='None', marker=marker, markersize=markersize,
8058+
x, y, linestyle='none', marker=marker, markersize=markersize,
80598059
**kwargs)
80608060
self.add_line(ret)
80618061
nr, nc = Z.shape

‎lib/matplotlib/collections.py

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.py
+85-64Lines changed: 85 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"antialiased": ["antialiaseds", "aa"],
2929
"edgecolor": ["edgecolors", "ec"],
3030
"facecolor": ["facecolors", "fc"],
31-
"linestyle": ["linestyles", "dashes", "ls"],
31+
"linestyle": ["linestyles", "ls"],
3232
"linewidth": ["linewidths", "lw"],
3333
"offset_transform": ["transOffset"],
3434
})
@@ -79,7 +79,7 @@ def __init__(self, *,
7979
edgecolors=None,
8080
facecolors=None,
8181
linewidths=None,
82-
linestyles='solid',
82+
linestyles='-',
8383
capstyle=None,
8484
joinstyle=None,
8585
antialiaseds=None,
@@ -104,15 +104,8 @@ def __init__(self, *,
104104
Face color for each patch making up the collection.
105105
linewidths : float or list of floats, default: :rc:`patch.linewidth`
106106
Line width for each patch making up the collection.
107-
linestyles : str or tuple or list thereof, default: 'solid'
108-
Valid strings are ['solid', 'dashed', 'dashdot', 'dotted', '-',
109-
'--', '-.', ':']. Dash tuples should be of the form::
110-
111-
(offset, onoffseq),
112-
113-
where *onoffseq* is an even length tuple of on and off ink lengths
114-
in points. For examples, see
115-
:doc:`/gallery/lines_bars_and_markers/linestyles`.
107+
linestyles : str or tuple or list thereof, default: '-'
108+
Line style or list of line styles. See `set_linestyle` for details.
116109
capstyle : `.CapStyle`-like, default: :rc:`patch.capstyle`
117110
Style to use for capping lines for all paths in the collection.
118111
Allowed values are %(CapStyle)s.
@@ -156,11 +149,12 @@ def __init__(self, *,
156149
cm.ScalarMappable.__init__(self, norm, cmap)
157150
# list of un-scaled dash patterns
158151
# this is needed scaling the dash pattern by linewidth
159-
self._us_linestyles = [(0, None)]
152+
self._unscaled_dash_patterns = [(0, None)]
160153
# list of dash patterns
161-
self._linestyles = [(0, None)]
154+
self._dash_patterns = [(0, None)]
155+
self._linestyles = ['-']
162156
# list of unbroadcast/scaled linewidths
163-
self._us_lw = [0]
157+
self._unscaled_lw = [0]
164158
self._linewidths = [0]
165159

166160
self._gapcolor = None # Currently only used by LineCollection.
@@ -379,7 +373,7 @@ def draw(self, renderer):
379373
if (len(paths) == 1 and len(trans) <= 1 and
380374
len(facecolors) == 1 and len(edgecolors) == 1 and
381375
len(self._linewidths) == 1 and
382-
all(ls[1] is None for ls in self._linestyles) and
376+
all(dash[1] is None for dash in self._dash_patterns) and
383377
len(self._antialiaseds) == 1 and len(self._urls) == 1 and
384378
self.get_hatch() is None):
385379
if len(trans):
@@ -400,7 +394,7 @@ def draw(self, renderer):
400394
if do_single_path_optimization:
401395
gc.set_foreground(tuple(edgecolors[0]))
402396
gc.set_linewidth(self._linewidths[0])
403-
gc.set_dashes(*self._linestyles[0])
397+
gc.set_dashes(*self._dash_patterns[0])
404398
gc.set_antialiased(self._antialiaseds[0])
405399
gc.set_url(self._urls[0])
406400
renderer.draw_markers(
@@ -422,7 +416,7 @@ def draw(self, renderer):
422416
gc, transform.frozen(), paths,
423417
self.get_transforms(), offsets, offset_trf,
424418
self.get_facecolor(), self.get_edgecolor(),
425-
self._linewidths, self._linestyles,
419+
self._linewidths, self._dash_patterns,
426420
self._antialiaseds, self._urls,
427421
"screen") # offset_position, kept for backcompat.
428422

@@ -579,54 +573,80 @@ def set_linewidth(self, lw):
579573
if lw is None:
580574
lw = self._get_default_linewidth()
581575
# get the un-scaled/broadcast lw
582-
self._us_lw = np.atleast_1d(lw)
576+
self._unscaled_lw = np.atleast_1d(lw)
583577

584578
# scale all of the dash patterns.
585-
self._linewidths, self._linestyles = self._bcast_lwls(
586-
self._us_lw, self._us_linestyles)
579+
self._linewidths, self._dash_patterns = self._bcast_lwls(
580+
self._unscaled_lw, self._unscaled_dash_patterns)
587581
self.stale = True
588582

589583
def set_linestyle(self, ls):
590584
"""
591-
Set the linestyle(s) for the collection.
585+
Set the line style(s) for the collection.
586+
587+
Parameters
588+
----------
589+
ls : str or tuple or list thereof
590+
The line style. Possible values:
592591
593-
=========================== =================
594-
linestyle description
595-
=========================== =================
596-
``'-'`` or ``'solid'`` solid line
597-
``'--'`` or ``'dashed'`` dashed line
598-
``'-.'`` or ``'dashdot'`` dash-dotted line
599-
``':'`` or ``'dotted'`` dotted line
600-
=========================== =================
592+
- A string:
601593
602-
Alternatively a dash tuple of the following form can be provided::
594+
========================================== =================
595+
linestyle description
596+
========================================== =================
597+
``'-'`` or ``'solid'`` solid line
598+
``'--'`` or ``'dashed'`` dashed line
599+
``'-.'`` or ``'dashdot'`` dash-dotted line
600+
``':'`` or ``'dotted'`` dotted line
601+
``'none'``, ``'None'``, ``' '``, or ``''`` draw nothing
602+
========================================== =================
603603
604-
(offset, onoffseq),
604+
- Alternatively a dash tuple of the following form can be
605+
provided::
605606
606-
where ``onoffseq`` is an even length tuple of on and off ink in points.
607+
(offset, onoffseq)
607608
608-
Parameters
609-
----------
610-
ls : str or tuple or list thereof
611-
Valid values for individual linestyles include {'-', '--', '-.',
612-
':', '', (offset, on-off-seq)}. See `.Line2D.set_linestyle` for a
613-
complete description.
609+
where ``onoffseq`` is an even length tuple of on and off ink
610+
in points.
611+
612+
If a single value is provided, this applies to all objects in the
613+
collection. A list can be provided to set different line styles to
614+
different objects.
615+
616+
For examples see :doc:`/gallery/lines_bars_and_markers/linestyles`.
617+
618+
The ``'dashed'``, ``'dashdot'``, and ``'dotted'`` line styles are
619+
controlled by :rc:`lines.dashed_pattern`,
620+
:rc:`lines.dashdot_pattern`, and :rc:`lines.dotted_pattern`,
621+
respectively.
614622
"""
615-
try:
616-
dashes = [mlines._get_dash_pattern(ls)]
617-
except ValueError:
623+
if isinstance(ls, str):
624+
dashes, ls_norm = map(list, zip(mlines._get_dash_pattern(ls)))
625+
else:
618626
try:
619-
dashes = [mlines._get_dash_pattern(x) for x in ls]
620-
except ValueError as err:
621-
emsg = f'Do not know how to convert {ls!r} to dashes'
622-
raise ValueError(emsg) from err
627+
dashes, ls_norm = map(list, zip(mlines._get_dash_pattern(ls)))
628+
except ValueError:
629+
dashes, ls_norm = map(
630+
list, zip(*[mlines._get_dash_pattern(x) for x in ls]))
623631

624632
# get the list of raw 'unscaled' dash patterns
625-
self._us_linestyles = dashes
633+
self._unscaled_dash_patterns = dashes
626634

627635
# broadcast and scale the lw and dash patterns
628-
self._linewidths, self._linestyles = self._bcast_lwls(
629-
self._us_lw, self._us_linestyles)
636+
self._linewidths, self._dash_patterns = self._bcast_lwls(
637+
self._unscaled_lw, self._unscaled_dash_patterns)
638+
self._linestyles = ls_norm
639+
640+
# Dashes used to be an alias of linestyle
641+
set_dashes = set_linestyle
642+
643+
def get_dashes(self):
644+
"""
645+
Return the dash patterns.
646+
647+
.. versionadded:: 3.8
648+
"""
649+
return self._dash_patterns
630650

631651
@_docstring.interpd
632652
def set_capstyle(self, cs):
@@ -827,7 +847,12 @@ def get_linewidth(self):
827847
return self._linewidths
828848

829849
def get_linestyle(self):
830-
return self._linestyles
850+
_api.warn_external(
851+
"Collection.get_linestyle will change return type from a list of dash "
852+
"pattern to a list of linestyle strings. This is consistent with Line2D. "
853+
"To get the previous result now and in the future without this warning, "
854+
"use get_dashes.")
855+
return self.get_dashes()
831856

832857
def _set_mappable_flags(self):
833858
"""
@@ -918,8 +943,10 @@ def update_from(self, other):
918943
self._original_facecolor = other._original_facecolor
919944
self._facecolors = other._facecolors
920945
self._linewidths = other._linewidths
946+
self._unscaled_lw = other._unscaled_lw
921947
self._linestyles = other._linestyles
922-
self._us_linestyles = other._us_linestyles
948+
self._unscaled_dash_patterns = other._unscaled_dash_patterns
949+
self._dash_patterns = other._dash_patterns
923950
self._pickradius = other._pickradius
924951
self._hatch = other._hatch
925952

@@ -1528,11 +1555,11 @@ def _get_inverse_paths_linestyles(self):
15281555
to nans to prevent drawing an inverse line.
15291556
"""
15301557
path_patterns = [
1531-
(mpath.Path(np.full((1, 2), np.nan)), ls)
1532-
if ls == (0, None) else
1533-
(path, mlines._get_inverse_dash_pattern(*ls))
1534-
for (path, ls) in
1535-
zip(self._paths, itertools.cycle(self._linestyles))]
1558+
(mpath.Path(np.full((1, 2), np.nan)), dash_patterns)
1559+
if dash_patterns == (0, None) else
1560+
(path, mlines._get_inverse_dash_pattern(*dash_patterns))
1561+
for (path, dash_patterns) in
1562+
zip(self._paths, itertools.cycle(self._dash_patterns))]
15361563

15371564
return zip(*path_patterns)
15381565

@@ -1555,7 +1582,7 @@ def __init__(self,
15551582
linelength=1,
15561583
linewidth=None,
15571584
color=None,
1558-
linestyle='solid',
1585+
linestyle='-',
15591586
antialiased=None,
15601587
**kwargs
15611588
):
@@ -1578,14 +1605,8 @@ def __init__(self,
15781605
The line width of the event lines, in points.
15791606
color : color or list of colors, default: :rc:`lines.color`
15801607
The color of the event lines.
1581-
linestyle : str or tuple or list thereof, default: 'solid'
1582-
Valid strings are ['solid', 'dashed', 'dashdot', 'dotted',
1583-
'-', '--', '-.', ':']. Dash tuples should be of the form::
1584-
1585-
(offset, onoffseq),
1586-
1587-
where *onoffseq* is an even length tuple of on and off ink
1588-
in points.
1608+
linestyle : str or tuple or list thereof, default: '-'
1609+
Line style or list of line styles. See `set_linestyle` for details.
15891610
antialiased : bool or list thereof, default: :rc:`lines.antialiased`
15901611
Whether to use antialiasing for drawing the lines.
15911612
**kwargs

‎lib/matplotlib/collections.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.pyi
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import numpy as np
1111
from numpy.typing import ArrayLike
1212
from collections.abc import Callable, Iterable, Sequence
1313
from typing import Literal
14-
from .typing import ColorType, LineStyleType, CapStyleType, JoinStyleType
14+
from .typing import ColorType, LineStyleType, CapStyleType, JoinStyleType, DashPatternType
1515

1616
class Collection(artist.Artist, cm.ScalarMappable):
1717
def __init__(
@@ -63,6 +63,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
6363
def set_alpha(self, alpha: float | Sequence[float] | None) -> None: ...
6464
def get_linewidth(self) -> float | Sequence[float]: ...
6565
def get_linestyle(self) -> LineStyleType | Sequence[LineStyleType]: ...
66+
def get_dashes(self) -> DashPatternType | Sequence[DashPatternType]: ...
6667
def update_scalarmappable(self) -> None: ...
6768
def get_fill(self) -> bool: ...
6869
def update_from(self, other: Artist) -> None: ...

‎lib/matplotlib/legend_handler.py

Copy file name to clipboardExpand all lines: lib/matplotlib/legend_handler.py
+1-1Lines changed: 1 addition & 1 deletion
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._unscaled_dash_patterns[0]
411411
color = orig_handle.get_colors()[0]
412412
legend_handle.set_color(color)
413413
legend_handle.set_linestyle(dashes)

0 commit comments

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