@@ -393,33 +393,32 @@ def test_auto_orient():
393
393
categorical = ["a" , "a" , "b" , "b" ]
394
394
numerical = [1 , 2 , 3 , 4 ]
395
395
396
- pattern_x_or_y = [
397
- (numerical , None , "h" ), # auto
398
- (categorical , None , "h" ), # auto
399
- (None , categorical , "v" ), # auto/default
400
- (None , numerical , "v" ), # auto/default
401
- ]
396
+ auto_orientable = [px .scatter , px .line , px .area , px .violin , px .box , px .strip ]
397
+ auto_orientable += [px .bar , px .funnel , px .histogram ]
402
398
403
399
pattern_x_and_y = [
404
400
(numerical , categorical , "h" ), # auto
405
401
(categorical , numerical , "v" ), # auto/default
406
402
(categorical , categorical , "v" ), # default
407
403
(numerical , numerical , "v" ), # default
408
404
]
409
-
410
- for fn in [px .violin , px .box , px .strip , px .bar , px .funnel ]:
411
- for x , y , result in pattern_x_or_y :
405
+ for fn in auto_orientable :
406
+ for x , y , result in pattern_x_and_y :
412
407
assert fn (x = x , y = y ).data [0 ].orientation == result
413
408
414
- # these ones are the opposite of the ones above in the "or" cases
415
- for fn in [px .area , px .histogram ]:
416
- for x , y , result in pattern_x_or_y :
417
- assert fn (x = x , y = y ).data [0 ].orientation != result
409
+ pattern_x_or_y = [
410
+ (numerical , None , "h" ), # auto
411
+ (categorical , None , "h" ), # auto
412
+ (None , categorical , "v" ), # auto/default
413
+ (None , numerical , "v" ), # auto/default
414
+ ]
418
415
419
- # all behave the same for the "and" cases
420
- for fn in [px .violin , px .box , px .strip , px .bar , px .funnel , px .area , px .histogram ]:
421
- for x , y , result in pattern_x_and_y :
422
- assert fn (x = x , y = y ).data [0 ].orientation == result
416
+ for fn in auto_orientable :
417
+ for x , y , result in pattern_x_or_y :
418
+ if fn == px .histogram or (fn == px .bar and categorical in [x , y ]):
419
+ assert fn (x = x , y = y ).data [0 ].orientation != result
420
+ else :
421
+ assert fn (x = x , y = y ).data [0 ].orientation == result
423
422
424
423
assert px .histogram (x = numerical , nbins = 5 ).data [0 ].nbinsx == 5
425
424
assert px .histogram (y = numerical , nbins = 5 ).data [0 ].nbinsy == 5
@@ -465,3 +464,69 @@ def test_auto_boxlike_overlay():
465
464
for fn , mode in fn_and_mode :
466
465
for x , y , color , result in pattern :
467
466
assert fn (df , x = x , y = y , color = color ).layout [mode ] == result
467
+
468
+
469
+ def test_x_or_y ():
470
+ categorical = ["a" , "a" , "b" , "b" ]
471
+ numerical = [1 , 2 , 3 , 4 ]
472
+ constant = [1 , 1 , 1 , 1 ]
473
+ range_4 = [0 , 1 , 2 , 3 ]
474
+ index = [11 , 12 , 13 , 14 ]
475
+ numerical_df = pd .DataFrame (dict (col = numerical ), index = index )
476
+ categorical_df = pd .DataFrame (dict (col = categorical ), index = index )
477
+ scatter_like = [px .scatter , px .line , px .area ]
478
+ bar_like = [px .bar ]
479
+
480
+ for fn in scatter_like + bar_like :
481
+ fig = fn (x = numerical )
482
+ assert list (fig .data [0 ].x ) == numerical
483
+ assert list (fig .data [0 ].y ) == range_4
484
+ assert fig .data [0 ].orientation == "h"
485
+ fig = fn (y = numerical )
486
+ assert list (fig .data [0 ].x ) == range_4
487
+ assert list (fig .data [0 ].y ) == numerical
488
+ assert fig .data [0 ].orientation == "v"
489
+ fig = fn (numerical_df , x = "col" )
490
+ assert list (fig .data [0 ].x ) == numerical
491
+ assert list (fig .data [0 ].y ) == index
492
+ assert fig .data [0 ].orientation == "h"
493
+ fig = fn (numerical_df , y = "col" )
494
+ assert list (fig .data [0 ].x ) == index
495
+ assert list (fig .data [0 ].y ) == numerical
496
+ assert fig .data [0 ].orientation == "v"
497
+
498
+ for fn in scatter_like :
499
+ fig = fn (x = categorical )
500
+ assert list (fig .data [0 ].x ) == categorical
501
+ assert list (fig .data [0 ].y ) == range_4
502
+ assert fig .data [0 ].orientation == "h"
503
+ fig = fn (y = categorical )
504
+ assert list (fig .data [0 ].x ) == range_4
505
+ assert list (fig .data [0 ].y ) == categorical
506
+ assert fig .data [0 ].orientation == "v"
507
+ fig = fn (categorical_df , x = "col" )
508
+ assert list (fig .data [0 ].x ) == categorical
509
+ assert list (fig .data [0 ].y ) == index
510
+ assert fig .data [0 ].orientation == "h"
511
+ fig = fn (categorical_df , y = "col" )
512
+ assert list (fig .data [0 ].x ) == index
513
+ assert list (fig .data [0 ].y ) == categorical
514
+ assert fig .data [0 ].orientation == "v"
515
+
516
+ for fn in bar_like :
517
+ fig = fn (x = categorical )
518
+ assert list (fig .data [0 ].x ) == categorical
519
+ assert list (fig .data [0 ].y ) == constant
520
+ assert fig .data [0 ].orientation == "v"
521
+ fig = fn (y = categorical )
522
+ assert list (fig .data [0 ].x ) == constant
523
+ assert list (fig .data [0 ].y ) == categorical
524
+ assert fig .data [0 ].orientation == "h"
525
+ fig = fn (categorical_df , x = "col" )
526
+ assert list (fig .data [0 ].x ) == categorical
527
+ assert list (fig .data [0 ].y ) == constant
528
+ assert fig .data [0 ].orientation == "v"
529
+ fig = fn (categorical_df , y = "col" )
530
+ assert list (fig .data [0 ].x ) == constant
531
+ assert list (fig .data [0 ].y ) == categorical
532
+ assert fig .data [0 ].orientation == "h"
0 commit comments