Skip to content

Commit df5f02e

Browse files
FBumannclaude
andcommitted
docs: add multi-trace add_secondary_y example to combining notebook
Demonstrates the legend fix: two figures sharing categorical legendgroups (e.g. country dimension) now produce 8 distinct legend entries instead of hiding the secondary's traces. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f586899 commit df5f02e

1 file changed

Lines changed: 68 additions & 29 deletions

File tree

docs/examples/combining.ipynb

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,37 @@
351351
"cell_type": "markdown",
352352
"id": "21",
353353
"metadata": {},
354+
"source": [
355+
"### Multi-Trace Figures\n",
356+
"\n",
357+
"When both base and secondary figures already split into multiple traces (e.g. a categorical xarray dimension), `add_secondary_y` keeps each side's traces visible in the legend. If the two figures share `legendgroup` names — common when they share a categorical dimension — those legendgroups are namespaced with each figure's y-axis title so every trace gets its own legend entry."
358+
]
359+
},
360+
{
361+
"cell_type": "code",
362+
"execution_count": null,
363+
"id": "22",
364+
"metadata": {},
365+
"outputs": [],
366+
"source": [
367+
"# All four countries on both axes — population on left, GDP per capita on right.\n",
368+
"# Both figures have legendgroups \"United States\", \"China\", ... — without\n",
369+
"# namespacing, the secondary's three traces would be deduped out of the legend.\n",
370+
"pop_fig = xpx(population).line(markers=True)\n",
371+
"gdp_fig = xpx(gdp_per_capita).line()\n",
372+
"\n",
373+
"combined = add_secondary_y(pop_fig, gdp_fig, secondary_y_title=\"GDP per Capita ($)\")\n",
374+
"combined.update_layout(\n",
375+
" title=\"Population (left) vs GDP per Capita (right) — all countries\",\n",
376+
" yaxis_title=\"Population\",\n",
377+
")\n",
378+
"combined"
379+
]
380+
},
381+
{
382+
"cell_type": "markdown",
383+
"id": "23",
384+
"metadata": {},
354385
"source": [
355386
"### With Animation\n",
356387
"\n",
@@ -360,7 +391,7 @@
360391
{
361392
"cell_type": "code",
362393
"execution_count": null,
363-
"id": "22",
394+
"id": "24",
364395
"metadata": {},
365396
"outputs": [],
366397
"source": [
@@ -378,7 +409,7 @@
378409
},
379410
{
380411
"cell_type": "markdown",
381-
"id": "23",
412+
"id": "25",
382413
"metadata": {},
383414
"source": [
384415
"### Static Secondary on Animated Base\n",
@@ -389,7 +420,7 @@
389420
{
390421
"cell_type": "code",
391422
"execution_count": null,
392-
"id": "24",
423+
"id": "26",
393424
"metadata": {},
394425
"outputs": [],
395426
"source": [
@@ -410,7 +441,7 @@
410441
},
411442
{
412443
"cell_type": "markdown",
413-
"id": "25",
444+
"id": "27",
414445
"metadata": {},
415446
"source": [
416447
"### With Facets\n",
@@ -421,7 +452,7 @@
421452
{
422453
"cell_type": "code",
423454
"execution_count": null,
424-
"id": "26",
455+
"id": "28",
425456
"metadata": {},
426457
"outputs": [],
427458
"source": [
@@ -439,7 +470,7 @@
439470
},
440471
{
441472
"cell_type": "markdown",
442-
"id": "27",
473+
"id": "29",
443474
"metadata": {},
444475
"source": [
445476
"## subplots\n",
@@ -450,7 +481,7 @@
450481
},
451482
{
452483
"cell_type": "markdown",
453-
"id": "28",
484+
"id": "30",
454485
"metadata": {},
455486
"source": [
456487
"### Different Variables Side by Side"
@@ -459,7 +490,7 @@
459490
{
460491
"cell_type": "code",
461492
"execution_count": null,
462-
"id": "29",
493+
"id": "31",
463494
"metadata": {},
464495
"outputs": [],
465496
"source": [
@@ -479,7 +510,7 @@
479510
},
480511
{
481512
"cell_type": "markdown",
482-
"id": "30",
513+
"id": "32",
483514
"metadata": {},
484515
"source": [
485516
"### 2x2 Grid\n",
@@ -490,7 +521,7 @@
490521
{
491522
"cell_type": "code",
492523
"execution_count": null,
493-
"id": "31",
524+
"id": "33",
494525
"metadata": {},
495526
"outputs": [],
496527
"source": [
@@ -507,7 +538,7 @@
507538
},
508539
{
509540
"cell_type": "markdown",
510-
"id": "32",
541+
"id": "34",
511542
"metadata": {},
512543
"source": [
513544
"### Mixed Chart Types\n",
@@ -519,7 +550,7 @@
519550
{
520551
"cell_type": "code",
521552
"execution_count": null,
522-
"id": "33",
553+
"id": "35",
523554
"metadata": {},
524555
"outputs": [],
525556
"source": [
@@ -535,7 +566,7 @@
535566
},
536567
{
537568
"cell_type": "markdown",
538-
"id": "34",
569+
"id": "36",
539570
"metadata": {},
540571
"source": [
541572
"### With Facets\n",
@@ -546,7 +577,7 @@
546577
{
547578
"cell_type": "code",
548579
"execution_count": null,
549-
"id": "35",
580+
"id": "37",
550581
"metadata": {},
551582
"outputs": [],
552583
"source": [
@@ -561,7 +592,7 @@
561592
},
562593
{
563594
"cell_type": "markdown",
564-
"id": "36",
595+
"id": "38",
565596
"metadata": {},
566597
"source": [
567598
"---\n",
@@ -573,7 +604,7 @@
573604
},
574605
{
575606
"cell_type": "markdown",
576-
"id": "37",
607+
"id": "39",
577608
"metadata": {},
578609
"source": [
579610
"### overlay: Mismatched Facet Structure\n",
@@ -584,7 +615,7 @@
584615
{
585616
"cell_type": "code",
586617
"execution_count": null,
587-
"id": "38",
618+
"id": "40",
588619
"metadata": {},
589620
"outputs": [],
590621
"source": [
@@ -602,7 +633,7 @@
602633
},
603634
{
604635
"cell_type": "markdown",
605-
"id": "39",
636+
"id": "41",
606637
"metadata": {},
607638
"source": [
608639
"### overlay: Animated Overlay on Static Base\n",
@@ -613,7 +644,7 @@
613644
{
614645
"cell_type": "code",
615646
"execution_count": null,
616-
"id": "40",
647+
"id": "42",
617648
"metadata": {},
618649
"outputs": [],
619650
"source": [
@@ -631,7 +662,7 @@
631662
},
632663
{
633664
"cell_type": "markdown",
634-
"id": "41",
665+
"id": "43",
635666
"metadata": {},
636667
"source": [
637668
"### overlay: Mismatched Animation Frames\n",
@@ -642,7 +673,7 @@
642673
{
643674
"cell_type": "code",
644675
"execution_count": null,
645-
"id": "42",
676+
"id": "44",
646677
"metadata": {},
647678
"outputs": [],
648679
"source": [
@@ -658,7 +689,7 @@
658689
},
659690
{
660691
"cell_type": "markdown",
661-
"id": "43",
692+
"id": "45",
662693
"metadata": {},
663694
"source": [
664695
"### add_secondary_y: Mismatched Facet Structure\n",
@@ -669,7 +700,7 @@
669700
{
670701
"cell_type": "code",
671702
"execution_count": null,
672-
"id": "44",
703+
"id": "46",
673704
"metadata": {},
674705
"outputs": [],
675706
"source": [
@@ -687,7 +718,7 @@
687718
},
688719
{
689720
"cell_type": "markdown",
690-
"id": "45",
721+
"id": "47",
691722
"metadata": {},
692723
"source": [
693724
"### add_secondary_y: Animated Secondary on Static Base\n",
@@ -698,7 +729,7 @@
698729
{
699730
"cell_type": "code",
700731
"execution_count": null,
701-
"id": "46",
732+
"id": "48",
702733
"metadata": {},
703734
"outputs": [],
704735
"source": [
@@ -716,7 +747,7 @@
716747
},
717748
{
718749
"cell_type": "markdown",
719-
"id": "47",
750+
"id": "49",
720751
"metadata": {},
721752
"source": [
722753
"### add_secondary_y: Mismatched Animation Frames"
@@ -725,7 +756,7 @@
725756
{
726757
"cell_type": "code",
727758
"execution_count": null,
728-
"id": "48",
759+
"id": "50",
729760
"metadata": {},
730761
"outputs": [],
731762
"source": [
@@ -741,7 +772,7 @@
741772
},
742773
{
743774
"cell_type": "markdown",
744-
"id": "49",
775+
"id": "51",
745776
"metadata": {},
746777
"source": [
747778
"## Summary\n",
@@ -761,8 +792,16 @@
761792
"name": "python3"
762793
},
763794
"language_info": {
795+
"codemirror_mode": {
796+
"name": "ipython",
797+
"version": 3
798+
},
799+
"file_extension": ".py",
800+
"mimetype": "text/x-python",
764801
"name": "python",
765-
"version": "3.12.0"
802+
"nbconvert_exporter": "python",
803+
"pygments_lexer": "ipython3",
804+
"version": "3.12.9"
766805
}
767806
},
768807
"nbformat": 4,

0 commit comments

Comments
 (0)