Skip to content

Commit e2e3238

Browse files
Add test for None separators between disjoint contour subpaths
1 parent 7097d6a commit e2e3238

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,38 @@ def test_contour_rings_are_closed():
371371
assert plotly_fig.data[1].y[0] == plotly_fig.data[1].y[-1]
372372

373373

374+
def test_disjoint_contour_subpaths_are_separated_by_none():
375+
"""Disjoint subpaths of one contour level are drawn in a single trace,
376+
separated by None so plotly does not connect them."""
377+
x = np.linspace(-3, 3, 61)
378+
X, Y = np.meshgrid(x, x)
379+
# two separate bumps: the 0.5 level is two disjoint rings in one path
380+
Z = np.exp(-((X + 1.5) ** 2 + Y**2)) + np.exp(-((X - 1.5) ** 2 + Y**2))
381+
fig, ax = plt.subplots()
382+
ax.contour(X, Y, Z, levels=[0.5])
383+
plotly_fig = tls.mpl_to_plotly(fig)
384+
385+
assert len(plotly_fig.data) == 1
386+
xs = list(plotly_fig.data[0].x)
387+
ys = list(plotly_fig.data[0].y)
388+
x_gaps = [i for i, v in enumerate(xs) if v is None]
389+
y_gaps = [i for i, v in enumerate(ys) if v is None]
390+
assert len(x_gaps) == 1
391+
assert x_gaps == y_gaps
392+
393+
gap = x_gaps[0]
394+
rings = [(xs[:gap], ys[:gap]), (xs[gap + 1 :], ys[gap + 1 :])]
395+
for ring_x, ring_y in rings:
396+
assert len(ring_x) > 2
397+
# each ring is closed
398+
assert ring_x[0] == ring_x[-1]
399+
assert ring_y[0] == ring_y[-1]
400+
# one ring around each bump, so no segment bridges the two
401+
left, right = sorted(rings, key=lambda ring: ring[0][0])
402+
assert max(left[0]) < 0
403+
assert min(right[0]) > 0
404+
405+
374406
def test_line_collection_date_xaxis():
375407
"""Line collections with date x-values must export date strings,
376408
not raw matplotlib date numbers."""

0 commit comments

Comments
 (0)