Skip to content

Conversation

@seisman
Copy link
Member

@seisman seisman commented Jul 26, 2025

This PR implements the Figure.scalebar method, which wraps the -L option in basemap/coast. Related to #4268.

The full syntax of -L is:

-L[g|j|J|n|x]refpoint+wlength[e|f|k|M|n|u][+aalign][+c[[slon/]slat]][+f][+jjustify][+l[label]][+odx[/dy]][+u][+v]

Here is a comparison of names used in PyGMT, GMT, and GMT.jl:

modifier PyGMT GMT GMT.jl
+w length length length
+a label_alignment align align
+c scale_at loc scale_at_lat
+l label label label
+u unit units units
+v vertical vertical vertical

Preview: https://pygmt-dev--4015.org.readthedocs.build/en/4015/api/generated/pygmt.Figure.scalebar.html#pygmt.Figure.scalebar

TODO in separate PRs

  • Update gallery examples using the new scalebar method
  • Deprecate map_scale in Figure.coast/Figure.basemap
  • Update the example in the paper

@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2025

Summary of changed images

This is an auto-generated report of images that have changed on the DVC remote

Status Path
added pygmt/tests/baseline/test_scalebar.png
added pygmt/tests/baseline/test_scalebar_cartesian.png
added pygmt/tests/baseline/test_scalebar_complete.png

Image diff(s)

Details

Added images

  • test_scalebar.png

  • test_scalebar_cartesian.png

  • test_scalebar_complete.png

Modified images

Path Old New

Report last updated at commit 0112a75

@seisman seisman changed the title WIP: Add Figure.scalebar to plot a scale bar on maps \Add Figure.scalebar to plot a scale bar on maps Dec 10, 2025
@seisman seisman changed the title \Add Figure.scalebar to plot a scale bar on maps Add Figure.scalebar to plot a scale bar on maps Dec 10, 2025
Base automatically changed from params/position to main December 11, 2025 01:54
@seisman seisman marked this pull request as ready for review December 11, 2025 11:49
@seisman seisman added the needs review This PR has higher priority and needs review. label Dec 11, 2025
@seisman seisman force-pushed the feature/scalebar branch 4 times, most recently from 0fdd8a3 to c86e8d5 Compare December 18, 2025 09:12
@seisman
Copy link
Member Author

seisman commented Dec 18, 2025

I think this PR is ready for review.

The key points we need to agree on (or explicitly discuss) are:

  • This method wraps the -L option of basemap/coast, so there is no corresponding standalone GMT module. For this reason, I prefer not to add common parameters such as projection, zsize, zscale, frame, or region to this method.
  • Parameter name for +a: align (GMT/GMT.jl) vs label_alignment (PyGMT). An alternative could be label_position.
  • Parameter name for +c: loc (GMT) vs scale_at_lat (GMT.jl) vs scale_position (PyGMT). Possible alternatives include scale_location or scale_lonlat. [Changed to scale_at following ChatGPT's recommendation]
  • Parameter name for +u: units (GMT/GMT.jl) vs unit (PyGMT). I think unit is the more appropriate choice.
  • The GMT configuration MAP_SCALE_HEIGHT controls the height of the fancy scalebar. Here it is exposed as a height parameter so users do not need to call pygmt.config.
  • Default position of the scalebar: by default, GMT places it at x0/0, which is often not ideal (see below). A more visually pleasing default is jBL+o0.2c/0.4c (bottom-left with offsets). While the optimal offsets depend on the plot dimensions, this works well for a reasonably sized (~15 cm wide) plot.
gmt begin map png
	gmt basemap -R100/120/30/40 -JM15c -Baf
	gmt basemap -L+w500
	gmt basemap -LjBL+o0.2/0.4+w500
gmt end show
map

seisman and others added 5 commits December 20, 2025 09:15
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Dec 27, 2025
@seisman
Copy link
Member Author

seisman commented Dec 27, 2025

Ping @GenericMappingTools/pygmt-maintainers for final reviews. I plan to merge it after 48 hours.

Comment on lines +56 to +58
Length of the scale bar in km. Append a suffix to specify different units. Valid
units are: **e**: meters; **f**: feet; **k**: kilometers; **M**: statute mile;
**n**: nautical miles; **u**: US Survey foot.
Copy link
Member

@yvonnefroehlich yvonnefroehlich Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we have different versions how we document the valid units:

$ grep "mile" */*
helpers/decorators.py:              **M**\ (iles), or **n**\ (autical miles) [Default is (m)\ **e**\ (ters)].
helpers/decorators.py:              is assumed to be given in meter, foot, km, mile, nautical mile or
src/grdproject.py:        **k** (kilometers), **M** (statute miles), **n** (nautical miles),
src/scalebar.py:        units are: **e**: meters; **f**: feet; **k**: kilometers; **M**: statute mile;
src/scalebar.py:        **n**: nautical miles; **u**: US Survey foot.
src/sphdistance.py:        **M** (miles), **n** (nautical miles), or **u** (survey feet).
src/x2sys_init.py:        - **m**: miles or mi/hr
src/x2sys_init.py:        - **n**: nautical miles or knots
grep: tests/baseline: Is a directory
grep: tests/data: Is a directory

I feel it would be nice to make this consistent (but we can do this in a separat PR, after we agreeded on one version).

Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Dec 29, 2025
@seisman seisman marked this pull request as draft December 29, 2025 02:58
@seisman seisman modified the milestones: 0.18.0, 0.19.0 Dec 29, 2025
@seisman
Copy link
Member Author

seisman commented Dec 29, 2025

I've bumped this PR to v0.19.0 following #4289 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Brand new feature

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants