diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f11346..9490525 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,6 @@ For the changes before the move out of plotly.py, see the [plotly.py changelog]( ### Changed - Move the converter out of plotly.py into the separate `matplotlylib` package. The git history of the converter moves with it. - Remove Python 3.8 and Python 3.9 support. matplotlylib requires Python 3.10 or later. + +### Fixed +- Fix `mpl_to_plotly` drawing matplotlib filled contour plots (`plt.contourf`) with spurious wedges joining the disjoint regions of each band by separating the subpaths of filled path collections with `None`, including on date x-axes diff --git a/matplotlylib/renderer.py b/matplotlylib/renderer.py index 41d23b0..c3b79e0 100644 --- a/matplotlylib/renderer.py +++ b/matplotlylib/renderer.py @@ -41,6 +41,62 @@ def _per_path(values, i, default): return values[i % n] if n else default +# In SVG paths, codes carry different numbers of vertices: +# M/L: 1, C: 3 (cubic curve), S: 2 (smooth/quad curve), Z: 0. +_VERTICES_PER_CODE = {"M": 1, "L": 1, "C": 3, "S": 2, "Z": 0} + + +def _split_subpaths(verts, codes): + """Split SVG path vertices into subpaths, one per "M" code. + + Returns a list of (vertices, closed) pairs; `closed` is True for + subpaths that have a "Z" code. + """ + subpaths = [] + current = [] + closed = False + vi = 0 + for c in codes: + step = _VERTICES_PER_CODE.get(c, 1) + if c == "M": + if current: + subpaths.append((current, closed)) + current = [verts[vi]] + closed = False + vi += 1 + elif c == "Z": + closed = True + else: + current.extend(verts[vi : vi + step]) + vi += step + if current: + subpaths.append((current, closed)) + return subpaths + + +def _join_subpaths(x, y, codes): + """Return the x and y values of a path's subpaths as two lists, with + None between consecutive subpaths. + + A closed subpath (Z code) ends with its first vertex repeated, since + plotly's lines mode draws only the segments between the given points. + Subpaths with fewer than 2 vertices are skipped. + """ + xs = [] + ys = [] + for sub, closed in _split_subpaths(list(zip(x, y)), codes): + if len(sub) < 2: + continue + if closed: + sub = sub + [sub[0]] + if xs: + xs.append(None) + ys.append(None) + xs.extend(v[0] for v in sub) + ys.extend(v[1] for v in sub) + return xs, ys + + def _convert_collection_dash(linestyle): """Convert a matplotlib collection line style to a plotly dash string. @@ -789,46 +845,12 @@ def flush(): for i, (verts, codes) in enumerate(props["paths"]): # a path may contain several disjoint lines (e.g. contour lines - # of the same level); separate disjoint subpaths with None so - # plotly does not connect them. - # In SVG paths, codes carry different numbers of vertices: - # M/L: 1, C: 3 (cubic curve), S: 2 (smooth/quad curve), Z: 0. - code_steps = {"M": 1, "L": 1, "C": 3, "S": 2, "Z": 0} - subpaths = [] - current = [] - closed = False - vi = 0 - for c in codes: - step = code_steps.get(c, 1) - if c == "M": - if current: - subpaths.append((current, closed)) - current = [verts[vi]] - closed = False - vi += 1 - elif c == "Z": - closed = True - else: - current.extend(verts[vi : vi + step]) - vi += step - if current: - subpaths.append((current, closed)) - path_x = [] - path_y = [] - for sub, closed in subpaths: - if len(sub) < 2: - continue - # a closed subpath (Z code) must be closed explicitly since - # plotly's lines mode does not close the loop - if closed: - sub = sub + [sub[0]] - sub_x = self._convert_x_dates([v[0] for v in sub]) - sub_y = [v[1] for v in sub] - if path_x: - path_x.append(None) - path_y.append(None) - path_x.extend(sub_x) - path_y.extend(sub_y) + # of the same level), separated by None + path_x, path_y = _join_subpaths( + self._convert_x_dates([v[0] for v in verts]), + [v[1] for v in verts], + codes, + ) if not path_x: continue @@ -851,7 +873,11 @@ def flush(): flush() def _draw_filled_path_collection(self, props): - """Draw a path collection (e.g. violin plot bodies) as filled polygons.""" + """Draw a path collection (e.g. violin plot bodies) as filled polygons. + + Each path becomes one trace, with None separating its disjoint + subpaths (e.g. the islands and holes of a contourf band). + """ facecolors = mpltools.convert_rgba_array(props["styles"]["facecolor"]) edgecolors = mpltools.convert_rgba_array(props["styles"]["edgecolor"]) linewidths = mpltools.convert_linewidth_array(props["styles"]["linewidth"]) @@ -860,10 +886,16 @@ def _draw_filled_path_collection(self, props): facecolor = _per_path(facecolors, i, "rgba(0,0,0,0)") edgecolor = _per_path(edgecolors, i, "rgba(0,0,0,0)") linewidth = _per_path(linewidths, i, 0) + x = self._convert_x_dates([v[0] for v in verts]) + y = [v[1] for v in verts] + xs, ys = _join_subpaths(x, y, codes) + if not xs and len(verts) > 0: + xs, ys = x, y + self.plotly_fig.add_trace( go.Scatter( - x=self._convert_x_dates([v[0] for v in verts]), - y=[v[1] for v in verts], + x=xs, + y=ys, mode="lines", line=go.scatter.Line( color=_export_color(edgecolor), width=linewidth diff --git a/tests/test_renderer.py b/tests/test_renderer.py index 6bc71d3..5747963 100644 --- a/tests/test_renderer.py +++ b/tests/test_renderer.py @@ -528,7 +528,8 @@ def test_contour_lines_convert(): def test_contourf_bands_render(): - """Contourf bands (multi-subpath collections) must render as fills.""" + """Contourf bands (multi-subpath collections) must render as fills with + None separating disjoint subpaths.""" x = np.linspace(-3, 3, 30) X, Y = np.meshgrid(x, x) fig, ax = plt.subplots() @@ -536,6 +537,64 @@ def test_contourf_bands_render(): plotly_fig = mpl_to_plotly(fig) filled = [t for t in plotly_fig.data if t.fill == "toself"] assert len(filled) > 0 + assert any(None in t.x for t in filled) + assert any(None in t.y for t in filled) + + +def test_contourf_bands_date_xaxis(): + """Contourf bands on a date x-axis export date strings, with None + separating disjoint subpaths.""" + dates = [ + datetime.datetime(2023, 1, 1) + datetime.timedelta(days=i) for i in range(30) + ] + y = np.linspace(-3, 3, 30) + X, Y = np.meshgrid(np.linspace(-3, 3, 30), y) + fig, ax = plt.subplots() + ax.contourf(dates, y, np.sin(X) * np.cos(Y), 10) + plotly_fig = mpl_to_plotly(fig) + filled = [t for t in plotly_fig.data if t.fill == "toself"] + split = [t for t in filled if None in t.x] + assert len(split) > 0 + for t in split: + assert [v is None for v in t.x] == [v is None for v in t.y] + assert all(isinstance(v, str) for v in t.x if v is not None) + + +def test_filled_path_collection_curve_codes(): + """Filled paths with Bezier curve codes export every vertex of each + subpath, so the curved outline spans its full extent and the following + subpath keeps its own vertices.""" + from matplotlib.collections import PatchCollection + from matplotlib.patches import PathPatch + from matplotlib.path import Path + + circle = Path.circle(center=(1, 1), radius=0.5) + square = Path( + [(2, 0.5), (3, 0.5), (3, 1.5), (2, 1.5), (2, 0.5)], + [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY], + ) + fig, ax = plt.subplots() + ax.add_collection( + PatchCollection([PathPatch(Path.make_compound_path(circle, square))]) + ) + ax.set_xlim(0, 4) + ax.set_ylim(0, 2) + plotly_fig = mpl_to_plotly(fig) + (trace,) = [t for t in plotly_fig.data if t.fill == "toself"] + split = list(trace.x).index(None) + circle_x = trace.x[:split] + circle_y = trace.y[:split] + assert min(circle_x) == pytest.approx(0.5, abs=0.02) + assert max(circle_x) == pytest.approx(1.5, abs=0.02) + assert min(circle_y) == pytest.approx(0.5, abs=0.02) + assert max(circle_y) == pytest.approx(1.5, abs=0.02) + assert list(zip(trace.x[split + 1 :], trace.y[split + 1 :])) == [ + (2, 0.5), + (3, 0.5), + (3, 1.5), + (2, 1.5), + (2, 0.5), + ] def test_axis_linecolor_defaults_to_black():