Skip to content

Commit a31cab1

Browse files
committed
Docs: add semantic legend guide and examples
Add a dedicated semantic legends section to the colorbars/legends guide with working examples for ax.cat_legend, ax.size_legend, ax.num_legend, and ax.geo_legend. The geo example now demonstrates generic polygons, country shorthand, and per-entry tuple overrides for country projection/resolution options. Also clean up the narrative text and convert the snippet into executable notebook cells.
1 parent 1710768 commit a31cab1

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

docs/colorbars_legends.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,103 @@
469469
ax = axs[1]
470470
ax.legend(hs2, loc="b", ncols=3, center=True, title="centered rows")
471471
axs.format(xlabel="xlabel", ylabel="ylabel", suptitle="Legend formatting demo")
472+
473+
# %% [raw] raw_mimetype="text/restructuredtext"
474+
# .. _ug_semantic_legends:
475+
# Semantic legends
476+
# ----------------
477+
#
478+
# Legends usually annotate artists already drawn on an axes, but sometimes you need
479+
# standalone semantic keys (categories, size scales, color levels, or geometry types).
480+
# UltraPlot provides helper methods that build these entries directly:
481+
#
482+
# * :meth:`~ultraplot.axes.Axes.cat_legend`
483+
# * :meth:`~ultraplot.axes.Axes.size_legend`
484+
# * :meth:`~ultraplot.axes.Axes.num_legend`
485+
# * :meth:`~ultraplot.axes.Axes.geo_legend`
486+
487+
# %%
488+
import cartopy.crs as ccrs
489+
import shapely.geometry as sg
490+
491+
fig, axs = uplt.subplots(
492+
ncols=2,
493+
nrows=2,
494+
refwidth=2.25,
495+
span=False,
496+
share=False,
497+
suptitle="Semantic legend helpers",
498+
)
499+
axs.format(grid=False)
500+
501+
ax = axs[0]
502+
ax.cat_legend(
503+
["A", "B", "C"],
504+
colors={"A": "red7", "B": "green7", "C": "blue7"},
505+
markers={"A": "o", "B": "s", "C": "^"},
506+
loc="c",
507+
frameon=False,
508+
)
509+
ax.format(title="cat_legend()")
510+
ax.axis("off")
511+
512+
ax = axs[1]
513+
ax.size_legend(
514+
[10, 50, 200],
515+
loc="c",
516+
title="Population",
517+
frameon=False,
518+
)
519+
ax.format(title="size_legend()")
520+
ax.axis("off")
521+
522+
ax = axs[2]
523+
ax.num_legend(
524+
vmin=0,
525+
vmax=1,
526+
n=5,
527+
cmap="viko",
528+
fmt="{:.2f}",
529+
loc="c",
530+
frameon=False,
531+
)
532+
ax.format(title="num_legend()")
533+
ax.axis("off")
534+
535+
poly1 = sg.Polygon([(0, 0), (2, 0), (1.2, 1.4)])
536+
ax = axs[3]
537+
ax.geo_legend(
538+
[
539+
("Triangle", "triangle"),
540+
("Triangle-ish", poly1),
541+
("Australia", "country:AU"),
542+
("Netherlands (Mercator)", "country:NLD", "mercator"),
543+
(
544+
"Netherlands (Lambert)",
545+
"country:NLD",
546+
{
547+
"country_proj": ccrs.LambertConformal(
548+
central_longitude=5,
549+
central_latitude=52,
550+
),
551+
"country_reso": "10m",
552+
"country_territories": False,
553+
"facecolor": "steelblue",
554+
"fill": True,
555+
},
556+
),
557+
],
558+
loc="c",
559+
ncols=1,
560+
handlesize=1.6,
561+
handletextpad=0.35,
562+
frameon=False,
563+
country_reso="10m",
564+
)
565+
ax.format(title="geo_legend()")
566+
ax.axis("off")
567+
568+
472569
# %% [raw] raw_mimetype="text/restructuredtext"
473570
# .. _ug_guides_decouple:
474571
#

0 commit comments

Comments
 (0)