Skip to content
Open
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
1 change: 1 addition & 0 deletions mplexporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def draw_collection(self, ax, collection,
styles = {'linewidth': collection.get_linewidths(),
'facecolor': collection.get_facecolors(),
'edgecolor': collection.get_edgecolors(),
'dasharray': utils.get_dasharray_list(collection),
'alpha': collection._alpha,
'zorder': collection.get_zorder()}

Expand Down
10 changes: 7 additions & 3 deletions mplexporter/renderers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ def _iter_path_collection(paths, path_transforms, offsets, styles):
if np.size(facecolor) == 0:
facecolor = ['none']

dasharray = styles.get('dasharray', None)
if dasharray is None or np.size(dasharray) == 0:
dasharray = ['none']

elements = [paths, path_transforms, offsets,
edgecolor, styles['linewidth'], facecolor]
edgecolor, styles['linewidth'], facecolor, dasharray]

it = itertools
return it.islice(py3k.zip(*py3k.map(it.cycle, elements)), N)
Expand Down Expand Up @@ -258,7 +262,7 @@ def draw_path_collection(self, paths, path_coordinates, path_transforms,

for tup in self._iter_path_collection(paths, path_transforms,
offsets, styles):
(path, path_transform, offset, ec, lw, fc) = tup
(path, path_transform, offset, ec, lw, fc, da) = tup
vertices, pathcodes = path
path_transform = transforms.Affine2D(path_transform)
vertices = path_transform.transform(vertices)
Expand All @@ -268,7 +272,7 @@ def draw_path_collection(self, paths, path_coordinates, path_transforms,
style = {"edgecolor": utils.export_color(ec),
"facecolor": utils.export_color(fc),
"edgewidth": lw,
"dasharray": "10,0",
"dasharray": da,
"alpha": styles['alpha'],
"zorder": styles['zorder']}
self.draw_path(data=vertices, coordinates=path_coordinates,
Expand Down
41 changes: 32 additions & 9 deletions mplexporter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ def _many_to_one(input_dict):
('', ' ', 'None', 'none'): None})


def _dasharray_from_linestyle(ls):
if ls is None:
return LINESTYLES['solid']
if isinstance(ls, tuple) and len(ls) == 2: # NOTE: No support for offset yet.
return ','.join(str(val) for val in ls[1]) if ls[1] else LINESTYLES['solid']
dasharray = LINESTYLES.get(ls, 'not found')
if dasharray == 'not found':
warnings.warn(f"line style '{ls}' not understood: defaulting to solid")
dasharray = LINESTYLES['solid']
return dasharray


def get_dasharray(obj):
"""Get an SVG dash array for the given matplotlib linestyle

Expand All @@ -59,16 +71,27 @@ def get_dasharray(obj):
dasharray : string
The HTML/SVG dasharray code associated with the object.
"""
if obj.__dict__.get('_dashSeq', None) is not None:
return ','.join(map(str, obj._dashSeq))
if dashseq := getattr(obj, '_dashSeq', None):
return _dasharray_from_linestyle(dashseq)

ls = obj.get_linestyle()
if isinstance(ls, (list, tuple)) and not isinstance(ls, str):
ls = ls[0] if len(ls) else None
return _dasharray_from_linestyle(ls)


def get_dasharray_list(collection):
"""Return a list of SVG dash arrays for a matplotlib Collection"""
linestyles = None
if hasattr(collection, "get_dashes"):
linestyles = collection.get_dashes()
elif hasattr(collection, "get_linestyle"):
linestyles = collection.get_linestyle()
else:
ls = obj.get_linestyle()
dasharray = LINESTYLES.get(ls, 'not found')
if dasharray == 'not found':
warnings.warn("line style '{0}' not understood: "
"defaulting to solid line.".format(ls))
dasharray = LINESTYLES['solid']
return dasharray
return None
if not isinstance(linestyles, (list, tuple)):
linestyles = [linestyles]
return [_dasharray_from_linestyle(ls) for ls in linestyles]


PATH_DICT = {Path.LINETO: 'L',
Expand Down