|
477 | 477 | # |
478 | 478 | # Legends usually annotate artists already drawn on an axes, but sometimes you need |
479 | 479 | # standalone semantic keys (categories, size scales, color levels, or geometry types). |
480 | | -# UltraPlot provides helper methods that build these entries directly: |
| 480 | +# UltraPlot provides helper methods that build these entries directly on both |
| 481 | +# axes and figures: |
481 | 482 | # |
482 | 483 | # * :meth:`~ultraplot.axes.Axes.entrylegend` |
483 | 484 | # * :meth:`~ultraplot.axes.Axes.catlegend` |
484 | 485 | # * :meth:`~ultraplot.axes.Axes.sizelegend` |
485 | 486 | # * :meth:`~ultraplot.axes.Axes.numlegend` |
486 | 487 | # * :meth:`~ultraplot.axes.Axes.geolegend` |
| 488 | +# * :meth:`~ultraplot.figure.Figure.entrylegend` |
| 489 | +# * :meth:`~ultraplot.figure.Figure.catlegend` |
| 490 | +# * :meth:`~ultraplot.figure.Figure.sizelegend` |
| 491 | +# * :meth:`~ultraplot.figure.Figure.numlegend` |
| 492 | +# * :meth:`~ultraplot.figure.Figure.geolegend` |
487 | 493 | # |
488 | 494 | # These helpers are useful whenever the legend should describe an encoding rather than |
489 | 495 | # mirror artists that already happen to be drawn. In practice there are two distinct |
|
513 | 519 | # |
514 | 520 | # The helpers are intentionally composable. Each one accepts ``add=False`` and returns |
515 | 521 | # ``(handles, labels)`` so you can merge semantic sections and pass the result through |
516 | | -# :meth:`~ultraplot.axes.Axes.legend` yourself. |
| 522 | +# :meth:`~ultraplot.axes.Axes.legend` or :meth:`~ultraplot.figure.Figure.legend` |
| 523 | +# yourself. |
517 | 524 | # |
518 | 525 | # .. code-block:: python |
519 | 526 | # |
|
568 | 575 | # |
569 | 576 | # .. code-block:: python |
570 | 577 | # |
| 578 | +# # Add semantic legends around an entire subplot group. |
| 579 | +# fig, axs = uplt.subplots(ncols=2) |
| 580 | +# fig.catlegend( |
| 581 | +# ["Control", "Treatment"], |
| 582 | +# colors={"Control": "blue7", "Treatment": "red7"}, |
| 583 | +# markers={"Control": "o", "Treatment": "^"}, |
| 584 | +# ref=axs, |
| 585 | +# loc="b", |
| 586 | +# title="Group", |
| 587 | +# ) |
| 588 | +# fig.sizelegend( |
| 589 | +# [10, 50, 200], |
| 590 | +# labels=["Small", "Medium", "Large"], |
| 591 | +# color="gray6", |
| 592 | +# ref=axs, |
| 593 | +# loc="r", |
| 594 | +# title="Population", |
| 595 | +# ) |
| 596 | +# |
| 597 | +# .. code-block:: python |
| 598 | +# |
571 | 599 | # # Compose multiple semantic helpers into one legend. |
572 | 600 | # size_handles, size_labels = ax.sizelegend( |
573 | 601 | # [10, 50, 200], |
|
685 | 713 | ax.axis("off") |
686 | 714 |
|
687 | 715 |
|
| 716 | +# %% |
| 717 | +fig, axs = uplt.subplots(ncols=2, refwidth=2.8, share=False) |
| 718 | +axs[0].scatter([0, 1, 2], [3, 1, 2], c=[0.2, 0.5, 0.8], s=[40, 120, 260]) |
| 719 | +axs[1].scatter([0, 1, 2], [2, 3, 1], c=[0.8, 0.4, 0.1], s=[60, 90, 220]) |
| 720 | +axs.format(title="Figure semantic legend helpers", grid=False) |
| 721 | + |
| 722 | +fig.catlegend( |
| 723 | + ["Control", "Treatment"], |
| 724 | + colors={"Control": "blue7", "Treatment": "red7"}, |
| 725 | + markers={"Control": "o", "Treatment": "^"}, |
| 726 | + ref=axs, |
| 727 | + loc="bottom", |
| 728 | + title="Group", |
| 729 | + frameon=False, |
| 730 | +) |
| 731 | +fig.sizelegend( |
| 732 | + [40, 120, 260], |
| 733 | + labels=["Small", "Medium", "Large"], |
| 734 | + color="gray6", |
| 735 | + ref=axs, |
| 736 | + loc="right", |
| 737 | + title="Size scale", |
| 738 | + frameon=False, |
| 739 | +) |
| 740 | + |
| 741 | + |
688 | 742 | # %% [raw] raw_mimetype="text/restructuredtext" |
689 | 743 | # .. _ug_guides_decouple: |
690 | 744 | # |
|
0 commit comments