diff --git a/mplexporter/exporter.py b/mplexporter/exporter.py index 52453ab..d84e560 100644 --- a/mplexporter/exporter.py +++ b/mplexporter/exporter.py @@ -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()} diff --git a/mplexporter/renderers/base.py b/mplexporter/renderers/base.py index 83543f5..588572f 100644 --- a/mplexporter/renderers/base.py +++ b/mplexporter/renderers/base.py @@ -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) @@ -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) @@ -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, diff --git a/mplexporter/utils.py b/mplexporter/utils.py index f9467d7..ccaf6af 100644 --- a/mplexporter/utils.py +++ b/mplexporter/utils.py @@ -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 @@ -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',