Repository navigation
Expand file tree
/
Copy pathtest_renderer.py
More file actions
645 lines (470 loc) · 20.5 KB
/
Copy pathtest_renderer.py
File metadata and controls
645 lines (470 loc) · 20.5 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
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_drawstyle_maps_to_line_shape():
cases = {
"steps-pre": "vh",
"steps": "vh",
"steps-post": "hv",
"steps-mid": "hvh",
}
for drawstyle, shape in cases.items():
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 0], drawstyle=drawstyle)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.data[0].line.shape == shape
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_axis_mirror_with_spines_and_ticks():
"""Test that mirror=True when both spines and ticks are visible on both sides."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
# Show all spines
ax.spines["top"].set_visible(True)
ax.spines["bottom"].set_visible(True)
ax.spines["left"].set_visible(True)
ax.spines["right"].set_visible(True)
# Show ticks on all sides
ax.tick_params(top=True, bottom=True, left=True, right=True)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.mirror == "ticks"
assert plotly_fig.layout.yaxis.mirror == "ticks"
def test_axis_mirror_with_ticks_only():
"""Test that mirror=False when spines are not visible on both sides."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
# Hide opposite spines
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
# Show ticks on all sides
ax.tick_params(top=True, bottom=True, left=True, right=True)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.mirror == False
assert plotly_fig.layout.yaxis.mirror == False
def test_axis_mirror_false_with_one_sided_ticks():
"""Test that mirror=True when ticks are only on one side but spines are
visible on both sides."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
# Default matplotlib behavior - ticks only on bottom and left
ax.tick_params(top=False, bottom=True, left=True, right=False)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.mirror == True
assert plotly_fig.layout.yaxis.mirror == True
def test_axis_mirror_mixed_configurations():
"""Test different configurations for x and y axes."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
# X-axis: spines and ticks on both sides (mirror="ticks")
ax.spines["top"].set_visible(True)
ax.spines["bottom"].set_visible(True)
ax.tick_params(top=True, bottom=True)
# Y-axis: spine only on one side (mirror=False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(True)
ax.tick_params(left=True, right=True)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.mirror == "ticks"
assert plotly_fig.layout.yaxis.mirror == False
def test_axis_showline_tied_to_main_spine():
"""Test that showline follows the main-side spine (bottom for x, left for y)."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
# Hide the mirror-side spines only
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.showline == True
assert plotly_fig.layout.yaxis.showline == True
def test_axis_showline_hidden_when_main_spine_hidden():
"""Test that showline is False when the main-side spine is hidden."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
# Hide the main-side spines but keep the mirror-side ones
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.showline == False
assert plotly_fig.layout.yaxis.showline == False
def test_ticks_hidden_when_mpl_main_ticks_hidden():
"""Test that tick markers are hidden when the mpl main-side ticks are hidden."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.tick_params(top=False, bottom=False, left=False, right=False)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.ticks == ""
assert plotly_fig.layout.yaxis.ticks == ""
def test_twinx_axis_position_and_ticks():
"""Test that twinx secondary y-axis is on the right with visible ticks and title."""
fig, ax1 = plt.subplots()
ax1.plot([0, 1, 2], [0, 1, 4])
ax1.set_ylabel("left axis")
ax2 = ax1.twinx()
ax2.plot([0, 1, 2], [10, 5, 2])
ax2.set_ylabel("right axis")
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.yaxis.side == "left"
assert plotly_fig.layout.yaxis.title.text == "left axis"
assert plotly_fig.layout.yaxis.ticks == "inside"
assert plotly_fig.layout.yaxis2.side == "right"
assert plotly_fig.layout.yaxis2.title.text == "right axis"
assert plotly_fig.layout.yaxis2.ticks == "inside"
assert plotly_fig.layout.yaxis2.overlaying == "y"
assert plotly_fig.layout.xaxis2.overlaying == "x"
assert plotly_fig.layout.xaxis2.visible is False
assert len(plotly_fig.data) == 2
def test_twiny_axis_position_and_ticks():
"""Test that twiny secondary x-axis is on the top with visible ticks and title."""
fig, ax1 = plt.subplots()
ax1.plot([0, 1, 2], [0, 1, 4])
ax1.set_xlabel("bottom axis")
ax2 = ax1.twiny()
ax2.plot([10, 5, 2], [0, 1, 4])
ax2.set_xlabel("top axis")
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.side == "bottom"
assert plotly_fig.layout.xaxis.title.text == "bottom axis"
assert plotly_fig.layout.xaxis.ticks == "inside"
assert plotly_fig.layout.xaxis2.side == "top"
assert plotly_fig.layout.xaxis2.title.text == "top axis"
assert plotly_fig.layout.xaxis2.ticks == "inside"
assert plotly_fig.layout.xaxis2.overlaying == "x"
assert plotly_fig.layout.yaxis2.overlaying == "y"
assert plotly_fig.layout.yaxis2.visible is False
assert len(plotly_fig.data) == 2
def test_right_axis_ticks_hidden_when_mpl_right_ticks_hidden():
"""Test that ticks are hidden on a right-side axis when right ticks are hidden in matplotlib."""
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax2.tick_params(right=False)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.yaxis2.side == "right"
assert plotly_fig.layout.yaxis2.ticks == ""
def test_sharex_stacked_lines_in_correct_subplots():
"""Test that vertically stacked subplots with sharex=True place lines in separate subplots."""
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot([1, 2, 3], [4, 5, 6])
ax2.plot([1, 2, 3], [10, 20, 30])
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) == 2
# First line belongs to the top subplot
assert plotly_fig.data[0].yaxis in (None, "y", "y1")
# Second line belongs to the bottom subplot
assert plotly_fig.data[1].yaxis == "y2"
# Distinct non-overlapping vertical domains (top above bottom)
assert plotly_fig.layout.yaxis.domain[0] > plotly_fig.layout.yaxis2.domain[1]
# The bottom subplot must not overlay the top subplot
assert plotly_fig.layout.yaxis2.overlaying is None
assert plotly_fig.layout.xaxis2.overlaying is None
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_no_legend_entries_for_internal_mpl_labels():
"""mpl internal labels (_nolegend_, _childN) must not become legend entries."""
fig, ax = plt.subplots()
ax.plot([0, 1, 2, 3], [0, 1, 0, 1], "b", [0, 1, 2, 3], [1, 0, 1, 0], "r--")
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.showlegend == False
assert all(t.name is None for t in plotly_fig.data)
def test_unlabeled_traces_hidden_from_legend_when_figure_has_legend():
"""Traces without labels must have showlegend=False when a figure has a legend."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1], label="Labeled line")
ax.plot([0, 1], [1, 0]) # Unlabeled line
ax.legend()
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.showlegend == True
assert len(plotly_fig.data) == 2
assert plotly_fig.data[0].name == "Labeled line"
assert plotly_fig.data[0].showlegend is not False
assert plotly_fig.data[1].name is None
assert plotly_fig.data[1].showlegend is False
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",
)
def test_tick_label_color_exports():
"""Tick label colors are exported to the plotly tickfont."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickfont.color == "#000000"
def test_dark_tick_label_color_exports():
"""Dark-background tick label colors are exported to the plotly
tickfont."""
with plt.style.context("dark_background"):
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickfont.color == "#FFFFFF"
def test_transparent_tick_label_color_exports():
"""Transparent tick label colors ('none') export as transparent rgba."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.tick_params(labelcolor="none")
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.layout.xaxis.tickfont.color == "rgba(0,0,0,0)"
assert plotly_fig.layout.yaxis.tickfont.color == "rgba(0,0,0,0)"