-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Stripey LineCollection
#24849
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
Stripey LineCollection
#24849
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
line segments). | ||
""" | ||
|
||
import itertools | ||
import math | ||
from numbers import Number | ||
import warnings | ||
|
@@ -163,6 +164,9 @@ def __init__(self, | |
# list of unbroadcast/scaled linewidths | ||
self._us_lw = [0] | ||
self._linewidths = [0] | ||
|
||
self._gapcolor = None # Currently only used by LineCollection. | ||
|
||
# Flags set by _set_mappable_flags: are colors from mapping an array? | ||
self._face_is_mapped = None | ||
self._edge_is_mapped = None | ||
|
@@ -406,6 +410,17 @@ def draw(self, renderer): | |
gc, paths[0], combined_transform.frozen(), | ||
mpath.Path(offsets), offset_trf, tuple(facecolors[0])) | ||
else: | ||
if self._gapcolor is not None: | ||
# First draw paths within the gaps. | ||
ipaths, ilinestyles = self._get_inverse_paths_linestyles() | ||
renderer.draw_path_collection( | ||
gc, transform.frozen(), ipaths, | ||
self.get_transforms(), offsets, offset_trf, | ||
[mcolors.to_rgba("none")], self._gapcolor, | ||
self._linewidths, ilinestyles, | ||
self._antialiaseds, self._urls, | ||
"screen") | ||
|
||
renderer.draw_path_collection( | ||
gc, transform.frozen(), paths, | ||
self.get_transforms(), offsets, offset_trf, | ||
|
@@ -1459,6 +1474,12 @@ def _get_default_edgecolor(self): | |
def _get_default_facecolor(self): | ||
return 'none' | ||
|
||
def set_alpha(self, alpha): | ||
# docstring inherited | ||
super().set_alpha(alpha) | ||
if self._gapcolor is not None: | ||
self.set_gapcolor(self._original_gapcolor) | ||
|
||
def set_color(self, c): | ||
""" | ||
Set the edgecolor(s) of the LineCollection. | ||
|
@@ -1479,6 +1500,53 @@ def get_color(self): | |
|
||
get_colors = get_color # for compatibility with old versions | ||
|
||
def set_gapcolor(self, gapcolor): | ||
""" | ||
Set a color to fill the gaps in the dashed line style. | ||
|
||
.. note:: | ||
|
||
Striped lines are created by drawing two interleaved dashed lines. | ||
There can be overlaps between those two, which may result in | ||
artifacts when using transparency. | ||
|
||
This functionality is experimental and may change. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. smallest little nit but I'd put only this very last sentence in the note and have everything else as just part of the docstring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The note here was lifted straight from the equivalent in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be changed there too but out of scope so I can put in that PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I remember of #23208, this went through a few iterations: started out with a warning about overlaps that don't look great with transparency. Then the warning was downgraded to a note, and the "experimental" bit added later. Given the amount of previous bikeshedding on this, I would be uncomfortable about changing it without a bit more discussion. That could happen in a follow-up PR, as you say. |
||
|
||
Parameters | ||
---------- | ||
gapcolor : color or list of colors or None | ||
The color with which to fill the gaps. If None, the gaps are | ||
unfilled. | ||
""" | ||
self._original_gapcolor = gapcolor | ||
self._set_gapcolor(gapcolor) | ||
|
||
def _set_gapcolor(self, gapcolor): | ||
if gapcolor is not None: | ||
gapcolor = mcolors.to_rgba_array(gapcolor, self._alpha) | ||
self._gapcolor = gapcolor | ||
self.stale = True | ||
|
||
def get_gapcolor(self): | ||
return self._gapcolor | ||
|
||
def _get_inverse_paths_linestyles(self): | ||
""" | ||
Returns the path and pattern for the gaps in the non-solid lines. | ||
|
||
This path and pattern is the inverse of the path and pattern used to | ||
construct the non-solid lines. For solid lines, we set the inverse path | ||
to nans to prevent drawing an inverse line. | ||
""" | ||
path_patterns = [ | ||
(mpath.Path(np.full((1, 2), np.nan)), ls) | ||
if ls == (0, None) else | ||
(path, mlines._get_inverse_dash_pattern(*ls)) | ||
for (path, ls) in | ||
zip(self._paths, itertools.cycle(self._linestyles))] | ||
|
||
return zip(*path_patterns) | ||
|
||
|
||
class EventCollection(LineCollection): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that
do_single_path_optimization
is onlyTrue
if all the lines are solidmatplotlib/lib/matplotlib/collections.py
Line 381 in fc5d316
so
gapcolor
is only meaningful after thiselse
.