Skip to content

Commit 632a72c

Browse files
committed
Allow empty linestyle for collections
1 parent e7fd79f commit 632a72c

13 files changed

+208
-122
lines changed

lib/matplotlib/axes/_axes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8030,7 +8030,7 @@ def spy(self, Z, precision=0, marker=None, markersize=None,
80308030
if 'linestyle' in kwargs:
80318031
raise _api.kwarg_error("spy", "linestyle")
80328032
ret = mlines.Line2D(
8033-
x, y, linestyle='None', marker=marker, markersize=markersize,
8033+
x, y, linestyle='none', marker=marker, markersize=markersize,
80348034
**kwargs)
80358035
self.add_line(ret)
80368036
nr, nc = Z.shape

lib/matplotlib/collections.py

+71-58
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,77 @@ 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+
def get_dashes(self):
641+
"""
642+
Return the dash patterns.
643+
644+
.. versionadded:: 3.8
645+
"""
646+
return self._dash_patterns
630647

631648
@_docstring.interpd
632649
def set_capstyle(self, cs):
@@ -918,8 +935,10 @@ def update_from(self, other):
918935
self._original_facecolor = other._original_facecolor
919936
self._facecolors = other._facecolors
920937
self._linewidths = other._linewidths
938+
self._unscaled_lw = other._unscaled_lw
921939
self._linestyles = other._linestyles
922-
self._us_linestyles = other._us_linestyles
940+
self._unscaled_dash_patterns = other._unscaled_dash_patterns
941+
self._dash_patterns = other._dash_patterns
923942
self._pickradius = other._pickradius
924943
self._hatch = other._hatch
925944

@@ -1555,7 +1574,7 @@ def __init__(self,
15551574
linelength=1,
15561575
linewidth=None,
15571576
color=None,
1558-
linestyle='solid',
1577+
linestyle='-',
15591578
antialiased=None,
15601579
**kwargs
15611580
):
@@ -1578,14 +1597,8 @@ def __init__(self,
15781597
The line width of the event lines, in points.
15791598
color : color or list of colors, default: :rc:`lines.color`
15801599
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.
1600+
linestyle : str or tuple or list thereof, default: '-'
1601+
Line style or list of line styles. See `set_linestyle` for details.
15891602
antialiased : bool or list thereof, default: :rc:`lines.antialiased`
15901603
Whether to use antialiasing for drawing the lines.
15911604
**kwargs

lib/matplotlib/collections.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
6464
def set_alpha(self, alpha: float | Sequence[float] | None) -> None: ...
6565
def get_linewidth(self) -> float | Sequence[float]: ...
6666
def get_linestyle(self) -> LineStyleType | Sequence[LineStyleType]: ...
67+
def get_dashes(self) -> LineStyleType | Sequence[LineStyleType]: ...
6768
def update_scalarmappable(self) -> None: ...
6869
def get_fill(self) -> bool: ...
6970
def update_from(self, other: Artist) -> None: ...

lib/matplotlib/legend_handler.py

+1-1
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)