Skip to content

Commit d18c2d2

Browse files
authored
Merge pull request #5799 from robertoffmoura/rm/relax-contour-grouping-tests
Make contour grouping matplotlylib tests version-aware
2 parents c4ecd22 + 1a9e0e6 commit d18c2d2

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import datetime
22

33
import numpy as np
4+
import matplotlib
45
import matplotlib.pyplot as plt
56
import matplotlib.dates as mdates
7+
from packaging.version import Version
68
import plotly.tools as tls
79

810

@@ -894,7 +896,8 @@ def test_contour_lines_not_in_legend():
894896

895897

896898
def test_consecutive_same_style_lines_grouped_into_one_trace():
897-
"""Consecutive paths with identical styles are grouped into a single trace."""
899+
"""Contour levels with identical styles share one trace on matplotlib 3.8
900+
and newer, which hands all levels over as a single collection."""
898901
x = np.linspace(-3, 3, 30)
899902
X, Y = np.meshgrid(x, x)
900903
fig, ax = plt.subplots()
@@ -910,15 +913,23 @@ def test_consecutive_same_style_lines_grouped_into_one_trace():
910913
)
911914
plotly_fig = tls.mpl_to_plotly(fig)
912915

913-
# All 4 levels share the same style, so they should be grouped into 1 trace
914-
assert len(plotly_fig.data) == 1
915-
trace = plotly_fig.data[0]
916-
# The trace combines the distinct levels separated by None
917-
assert trace.x.count(None) >= 3
916+
# All 4 levels share the same style
917+
assert (
918+
len({(t.line.color, t.line.width, t.line.dash) for t in plotly_fig.data}) == 1
919+
)
920+
if Version(matplotlib.__version__) >= Version("3.8"):
921+
# matplotlib >= 3.8 hands the levels over as one collection, so they
922+
# are grouped into 1 trace with the distinct levels separated by None
923+
assert len(plotly_fig.data) == 1
924+
assert plotly_fig.data[0].x.count(None) >= 3
925+
else:
926+
# older matplotlib emits one collection per level
927+
assert len(plotly_fig.data) == 4
918928

919929

920930
def test_mixed_style_lines_group_consecutive_matches():
921-
"""Paths are grouped by consecutive matching style (color, width, dash)."""
931+
"""Consecutive paths with matching styles share a trace and differing
932+
styles start a new one on matplotlib 3.8 and newer."""
922933
x = np.linspace(-3, 3, 30)
923934
X, Y = np.meshgrid(x, x)
924935
fig, ax = plt.subplots()
@@ -933,7 +944,10 @@ def test_mixed_style_lines_group_consecutive_matches():
933944
)
934945
plotly_fig = tls.mpl_to_plotly(fig)
935946

936-
# 2 dashed levels grouped into 1 trace, 2 solid levels grouped into 1 trace -> 2 traces total
937-
assert len(plotly_fig.data) == 2
938-
assert plotly_fig.data[0].line.dash != "solid"
939-
assert plotly_fig.data[1].line.dash == "solid"
947+
dashes = [t.line.dash for t in plotly_fig.data]
948+
if Version(matplotlib.__version__) >= Version("3.8"):
949+
# 2 dashed levels grouped into 1 trace, 2 solid levels grouped into 1 trace
950+
assert dashes == ["5.55px,2.4px", "solid"]
951+
else:
952+
# older matplotlib emits one collection per level
953+
assert dashes == ["5.55px,2.4px"] * 2 + ["solid"] * 2

0 commit comments

Comments
 (0)