Repository navigation
Expand file tree
/
Copy pathtest_renderer.py
More file actions
368 lines (275 loc) · 11.6 KB
/
Copy pathtest_renderer.py
File metadata and controls
368 lines (275 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import datetime
import numpy as np
import matplotlib.pyplot as plt
import plotly.tools as tls
def test_native_legend_enabled_when_matplotlib_legend_present():
"""Test that when matplotlib legend is present, Plotly uses native legend."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1], label="Line 1")
ax.plot([0, 1], [1, 0], label="Line 2")
ax.legend()
plotly_fig = tls.mpl_to_plotly(fig)
# Should enable native legend
assert plotly_fig.layout.showlegend == True
# Should have 2 traces with names
assert len(plotly_fig.data) == 2
assert plotly_fig.data[0].name == "Line 1"
assert plotly_fig.data[1].name == "Line 2"
def test_no_fake_legend_shapes_with_native_legend():
"""Test that fake legend shapes are not created when using native legend."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1], "o-", label="Data with markers")
ax.legend()
plotly_fig = tls.mpl_to_plotly(fig)
# Should use native legend
assert plotly_fig.layout.showlegend == True
# Should not create fake legend elements
assert len(plotly_fig.layout.shapes) == 0
assert len(plotly_fig.layout.annotations) == 0
def test_legend_disabled_when_no_matplotlib_legend():
"""Test that legend is not enabled when no matplotlib legend is present."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1], label="Line 1") # Has label but no legend() call
plotly_fig = tls.mpl_to_plotly(fig)
# Should not have showlegend explicitly set to True
# (Plotly's default behavior when no legend elements exist)
assert (
not hasattr(plotly_fig.layout, "showlegend")
or plotly_fig.layout.showlegend != True
)
def test_legend_disabled_when_matplotlib_legend_not_visible():
"""Test that legend is not enabled when no matplotlib legend is not visible."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1], label="Line 1")
legend = ax.legend()
legend.set_visible(False) # Hide the legend
plotly_fig = tls.mpl_to_plotly(fig)
# Should not enable legend when matplotlib legend is hidden
assert (
not hasattr(plotly_fig.layout, "showlegend")
or plotly_fig.layout.showlegend != True
)
def test_multiple_traces_native_legend():
"""Test native legend works with multiple traces of different types."""
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 0], "-", label="Line")
ax.plot([0, 1, 2], [1, 0, 1], "o", label="Markers")
ax.plot([0, 1, 2], [0.5, 0.5, 0.5], "s-", label="Line+Markers")
ax.legend()
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.showlegend == True
assert len(plotly_fig.data) == 3
assert plotly_fig.data[0].name == "Line"
assert plotly_fig.data[1].name == "Markers"
assert plotly_fig.data[2].name == "Line+Markers"
# Verify modes are correct
assert plotly_fig.data[0].mode == "lines"
assert plotly_fig.data[1].mode == "markers"
assert plotly_fig.data[2].mode == "lines+markers"
def test_violinplot_bodies_are_filled_polygons():
fig, ax = plt.subplots()
ax.violinplot(np.random.randn(100, 3))
plotly_fig = tls.mpl_to_plotly(fig)
bodies = [t for t in plotly_fig.data if t.fill == "toself" and len(t.x) > 100]
assert len(bodies) >= 3
def test_pcolor_rectangles_render():
x = np.linspace(-3, 3, 10)
X, Y = np.meshgrid(x, x)
fig, ax = plt.subplots()
ax.pcolor(X, Y, np.sin(X) * np.cos(Y))
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) == 100
assert all(len(t.x) >= 4 for t in plotly_fig.data)
def test_eventplot_segments_render():
fig, ax = plt.subplots()
ax.eventplot([np.random.randn(20) for _ in range(5)])
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) == 100
def test_stackplot_areas_render():
x = np.arange(10)
fig, ax = plt.subplots()
ax.stackplot(x, np.random.rand(10), np.random.rand(10), np.random.rand(10))
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) >= 3
def test_fill_between_renders():
x = np.linspace(0, 2 * np.pi, 50)
fig, ax = plt.subplots()
ax.fill_between(x, np.sin(x), np.cos(x))
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) >= 1
def test_collection_alpha():
"""Collection alpha is baked into the facecolor rgba by matplotlib. if
fillcolor has an alpha channel, the opacity field should not be set."""
x = np.linspace(0, 2 * np.pi, 50)
fig, ax = plt.subplots()
ax.fill_between(x, np.sin(x), np.cos(x), color="red", alpha=0.4)
plotly_fig = tls.mpl_to_plotly(fig)
trace = plotly_fig.data[0]
assert trace.fillcolor == "rgba(255,0,0,0.4)"
assert trace.opacity is None
def test_violin_body_default_alpha():
"""Violin bodies default to alpha=0.3 in matplotlib, which is
embedded in their facecolor rgba. If the alpha channel in fillcolor
is set, the opacity field should not be set."""
fig, ax = plt.subplots()
ax.violinplot(np.random.randn(100, 3))
plotly_fig = tls.mpl_to_plotly(fig)
bodies = [
t
for t in plotly_fig.data
if t.fill == "toself" and t.fillcolor == "rgba(31,119,180,0.3)"
]
assert len(bodies) >= 3
assert all(t.opacity is None for t in bodies)
def test_stem_plot_renders():
x = np.linspace(0, 2 * np.pi, 20)
fig, ax = plt.subplots()
ax.stem(x, np.sin(x))
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) >= 20
def test_contour_lines_convert():
"""Contour lines used to crash with an ndarray line width."""
x = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, x)
fig, ax = plt.subplots()
ax.contour(X, Y, np.sin(X) * np.cos(Y), 10)
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) > 0
def test_contourf_bands_render():
"""Contourf bands (multi-subpath collections) must render as fills."""
x = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, x)
fig, ax = plt.subplots()
ax.contourf(X, Y, np.sin(X) * np.cos(Y), 10)
plotly_fig = tls.mpl_to_plotly(fig)
filled = [t for t in plotly_fig.data if t.fill == "toself"]
assert len(filled) > 0
def test_filled_path_collection_date_xaxis():
"""Filled path collections with date x-values must export date strings,
not raw matplotlib date numbers."""
dates = [
datetime.datetime(2023, 1, 1) + datetime.timedelta(days=i) for i in range(10)
]
fig, ax = plt.subplots()
ax.fill_between(dates, np.sin(np.arange(10)), np.cos(np.arange(10)))
plotly_fig = tls.mpl_to_plotly(fig)
filled = [t for t in plotly_fig.data if t.fill == "toself"]
assert len(filled) >= 1
assert all(isinstance(x, str) for x in filled[0].x)
def test_background_colors_from_matplotlib_defaults():
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.plot_bgcolor == "#FFFFFF"
assert plotly_fig.layout.paper_bgcolor == "#FFFFFF"
def test_custom_background_colors_are_preserved():
fig, ax = plt.subplots()
fig.patch.set_facecolor("lightyellow")
ax.set_facecolor("lightgray")
ax.plot([0, 1], [0, 1])
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.plot_bgcolor == "#D3D3D3"
assert plotly_fig.layout.paper_bgcolor == "#FFFFE0"
def test_semitransparent_axes_background_preserved():
"""Axes backgrounds with alpha export as mpl-style rgba strings, which
must be passed through as-is, not re-parsed by export_color."""
fig, ax = plt.subplots()
ax.set_facecolor((0.1, 0.2, 0.3, 0.4))
ax.plot([0, 1], [0, 1])
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.plot_bgcolor == "rgba(26, 51, 76, 0.4)"
def test_histogram_converts():
"""Histograms must convert without error and keep bargap in plotly's
valid [0, 1] range; get_bar_gap can return a gap with floating point
noise for touching bars, which plotly rejects."""
# Seed 0 makes the first gap slightly negative (-4.4e-16)
rng = np.random.RandomState(0)
fig, ax = plt.subplots()
ax.hist(rng.randn(10000), 30)
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) == 1
assert plotly_fig.layout.bargap == 0
def test_line_color_is_valid_plotly_color():
"""Converted line colors are valid plotly color strings: plotly rejects
a space between 'rgba' and the opening parenthesis."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1], color="red")
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.data[0].line.color == "rgba(255, 0, 0, 1)"
def test_non_arithmetic_progression_xtickvals():
xticks = [0.01, 0.53, 0.75]
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.set_xticks(xticks)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickvals == tuple(xticks)
def test_non_arithmetic_progression_yticks():
yticks = [0.01, 0.53, 0.75]
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.set_yticks(yticks)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.yaxis.tickvals == tuple(yticks)
def test_non_arithmetic_progression_xticktext():
xtickvals = [0.01, 0.53, 0.75]
xticktext = ["Baseline", "param = 1", "param = 2"]
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.set_xticks(xtickvals, xticktext)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickvals == tuple(xtickvals)
assert plotly_fig.layout.xaxis.ticktext == tuple(xticktext)
def test_fixed_formatter_ticktext():
import matplotlib.ticker as ticker
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.xaxis.set_major_locator(ticker.FixedLocator([0.01, 0.53, 0.75]))
ax.xaxis.set_major_formatter(
ticker.FixedFormatter(["Baseline", "param = 1", "param = 2"])
)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickvals == (0.01, 0.53, 0.75)
assert plotly_fig.layout.xaxis.ticktext == ("Baseline", "param = 1", "param = 2")
def test_custom_date_xtickvals_are_converted():
"""Custom tick values on a date axis must be converted to date strings,
not left as raw matplotlib date numbers or datetime objects."""
dates = [datetime.datetime(2023, 1, i) for i in range(1, 11)]
fig, ax = plt.subplots()
ax.plot(dates, np.random.rand(10))
ax.set_xticks(dates[::3])
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickvals == (
"2023-01-01 00:00:00",
"2023-01-04 00:00:00",
"2023-01-07 00:00:00",
"2023-01-10 00:00:00",
)
def test_uneven_custom_date_xtickvals_are_converted():
"""Unevenly spaced custom date ticks must be converted to date strings."""
dates = [datetime.datetime(2023, 1, i) for i in range(1, 11)]
ticks = [datetime.datetime(2023, 1, i) for i in [1, 3, 6, 10]]
fig, ax = plt.subplots()
ax.plot(dates, np.random.rand(10))
ax.set_xticks(ticks)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickvals == (
"2023-01-01 00:00:00",
"2023-01-03 00:00:00",
"2023-01-06 00:00:00",
"2023-01-10 00:00:00",
)
def test_custom_date_xtickvals_given_as_numbers_are_converted():
"""Custom date ticks given as matplotlib date numbers must be converted
to date strings."""
import matplotlib.dates as mdates
dates = [datetime.datetime(2023, 1, i) for i in range(1, 11)]
fig, ax = plt.subplots()
ax.plot(dates, np.random.rand(10))
ax.set_xticks([mdates.date2num(d) for d in dates[::3]])
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickvals == (
"2023-01-01 00:00:00",
"2023-01-04 00:00:00",
"2023-01-07 00:00:00",
"2023-01-10 00:00:00",
)