Placing elements¶
The title, scale bar, north arrow, source line and legends all take a
position. They understand the same forms, so a position written for one
element works for any other. Without a position an element sits at its
default place (a corner of the map for the scale bar and the north arrow, above
the map for the title, below it for legends).
import mappyng as mp
m = (
mp.Map(gdf, bbox=[90000, 6040000, 1280000, 7150000])
+ mp.Basemap()
+ mp.ScaleBar(length=100_000, position={"lon": 3.3, "lat": 42.95})
+ mp.NorthArrow(position={"x": "92%", "y": "8%"})
)
A complete example is in the gallery: Placing elements on the map.
Forms of position¶
Form |
Meaning |
|---|---|
|
Fractions of the map area (the drawn extent, not the whole
document): |
|
The same, written as percentages. |
|
SVG pixels in the document. |
|
A point in the map CRS: metres for a Lambert-93 map, degrees for a lon/lat map. |
|
A point in another CRS, reprojected to the map CRS. |
|
A point in WGS 84 longitude/latitude, whatever the map CRS. |
Forms can be mixed freely from one element to the next. Within a single
position, write one form only: if several keys are present, geo is used
first, then lon/lat, then px/py, and x/y last, so the
others are ignored without a warning.
What the point designates¶
The point is the element’s anchor, which differs from one element to the next:
Element |
Anchor |
|---|---|
The left end of the bar. It extends to the right, over the length of the bar. |
|
The tip of the arrow head. The label sits above it. |
|
The start of the text, aligned by |
|
The baseline of the last line (the subtitle if there is one), aligned
by |
|
Legends |
The top-left corner of the legend, title included. The |
Anchoring to a place¶
Geographic positions tie an element to a place rather than to a spot on the page, so it stays put if you change the document size or the bounding box. A typical use is a scale bar over the sea, clear of the coast:
mp.ScaleBar(length=100_000, position={"lon": 3.3, "lat": 42.95})
lon/lat are always WGS 84. To use another CRS, give it explicitly:
mp.NorthArrow(position={"geo": (3_773_000, 2_229_000), "crs": "EPSG:3035"})
Finding the right coordinates is easiest by trial: render, look, move. The
resolve_position() function turns any position into
SVG pixels if you need to compute a placement in code.
What can go wrong¶
A malformed or impossible position raises a
LayoutError with a message saying why, instead of
silently placing the element at a default spot.
Position |
Problem |
|---|---|
|
Not an |
|
|
|
Not a number or a percentage. |
|
A CRS is needed to convert geographic coordinates. Set one on the
GeoDataFrame, or use |
A point on the hidden side of an orthographic globe |
The projection cannot represent it. |
In YAML¶
Positions are plain values, so they survive to_yaml and from_yaml:
chrome:
scale_bar:
length: 100000
position:
lon: 3.3
lat: 42.95
title:
text: Mortality
position:
x: 2%
y: -1%
Only the main map is a target for positions: the scale bar, north arrow, source,
title and legends belong to the main viewport. Cartouches and the zoom window
have their own frame and are placed with Insets and Zoom.