|
2 | 2 | Colorbar |
3 | 3 | ======== |
4 | 4 |
|
5 | | -The :meth:`pygmt.Figure.colorbar` method creates a color scalebar. |
6 | | -The colormap is set via the ``cmap`` parameter. A full list of available |
7 | | -color palette tables can be found at :gmt-docs:`reference/cpts.html`. |
8 | | -Use the ``frame`` parameter to add labels to the **x** and **y** axes |
9 | | -of the colorbar by appending **+l** followed by the desired text. To add |
10 | | -and adjust the annotations (**a**) and ticks (**f**) append the letter |
11 | | -followed by the desired interval. The placement of the colorbar is set |
12 | | -via the ``position`` parameter. There are the following options: |
13 | | -
|
14 | | -- **j/J**: placed inside/outside the plot bounding box using a |
15 | | - :doc:`2-character justification code </techref/justification_codes>`, e.g., |
16 | | - ``position="jTR"`` for Top Right. |
17 | | -- **g**: using map coordinates, e.g. ``position="g170/-45"`` for longitude |
18 | | - 170° East, latitude 45° South. |
19 | | -- **x**: using paper coordinates, e.g. ``position="x5c/7c"`` for 5 cm, 7 cm |
20 | | - from anchor point. |
21 | | -- **n**: using normalized (0-1) coordinates, e.g. ``position="n0.4/0.8"``. |
22 | | -
|
23 | | -Note that the anchor point defaults to Bottom Left (**BL**). Append ``+h`` to |
24 | | -``position`` to get a horizontal colorbar instead of a vertical one (``+v``). |
| 5 | +The :meth:`pygmt.Figure.colorbar` method creates a color scalebar. The colormap is set |
| 6 | +via the ``cmap`` parameter. A full list of available color palette tables can be found |
| 7 | +at :gmt-docs:`reference/cpts.html`. Use the ``frame`` parameter to add labels to the |
| 8 | +**x** and **y** axes of the colorbar by appending **+l** followed by the desired text. |
| 9 | +To add and adjust the annotations (**a**) and ticks (**f**) append the letter followed |
| 10 | +by the desired interval. The placement of the colorbar is set by passing a |
| 11 | +:class:`pygmt.params.Position` object to the ``position`` parameter. |
25 | 12 | """ |
26 | 13 |
|
27 | 14 | # %% |
28 | 15 | import pygmt |
| 16 | +from pygmt.params import Position |
29 | 17 |
|
30 | 18 | fig = pygmt.Figure() |
31 | 19 | fig.basemap(region=[0, 3, 6, 9], projection="x3c", frame=["af", "WSne+tColorbars"]) |
|
41 | 29 | # Create a colorbar showing the scientific rainbow - batlow |
42 | 30 | fig.colorbar( |
43 | 31 | cmap="SCM/batlow", |
44 | | - # Colorbar positioned at map coordinates (g) longitude/latitude 0.3/8.7, |
45 | | - # with a length/width (+w) of 4 cm by 0.5 cm, and plotted horizontally (+h) |
46 | | - position="g0.3/8.7+w4c/0.5c+h", |
| 32 | + # A horizontal colorbar positioned at map coordinates (0.3, 8.7), with a |
| 33 | + # length of 4 cm and a width of 0.5 cm. |
| 34 | + position=Position((0.3, 8.7), cstype="mapcoords"), |
| 35 | + length=4, |
| 36 | + width=0.5, |
| 37 | + orientation="horizontal", |
47 | 38 | box=True, |
48 | 39 | frame=["x+lTemperature", "y+l°C"], |
49 | 40 | scale=100, |
|
53 | 44 | # Create a colorbar suitable for surface topography - oleron |
54 | 45 | fig.colorbar( |
55 | 46 | cmap="SCM/oleron", |
56 | | - # Colorbar placed outside the plot bounding box (J) at Middle Right (MR), |
57 | | - # offset (+o) by 1 cm horizontally and 0 cm vertically from anchor point, |
58 | | - # with a length/width (+w) of 7 cm by 0.5 cm and a box for NaN values (+n) |
59 | | - position="JMR+o1c/0c+w7c/0.5c+n+mc", |
| 47 | + # Colorbar placed at Middle Right (MR) outside the plot bounding box, offset by 1 cm |
| 48 | + # horizontally and 0 cm vertically from anchor point, with a length of 7 cm and |
| 49 | + # width of 0.5 cm, and a rectangle for NaN values. |
60 | 50 | # Note that the label 'Elevation' is moved to the opposite side and plotted |
61 | | - # vertically as a column of text using '+mc' in the position parameter |
62 | | - # above |
| 51 | + # vertically as a column of characters. |
| 52 | + position=Position("MR", cstype="outside", offset=(1, 0)), |
| 53 | + length=7, |
| 54 | + width=0.5, |
| 55 | + nan=True, |
| 56 | + move_text=["annotations", "label"], |
| 57 | + label_as_column=True, |
63 | 58 | frame=["x+lElevation", "y+lm"], |
64 | 59 | scale=10, |
65 | 60 | ) |
|
76 | 71 | # Plot the colorbar |
77 | 72 | fig.colorbar( |
78 | 73 | cmap=True, # Use colormap set up above |
79 | | - # Colorbar placed inside the plot bounding box (j) in the Bottom Left (BL) corner, |
80 | | - # with an offset (+o) by 0.5 cm horizontally and 0.8 cm vertically from the anchor |
81 | | - # point, and plotted horizontally (+h) |
82 | | - position="jBL+o0.5c/0.8c+h", |
| 74 | + # Colorbar placed in the Bottom Left (BL) corner inside the plot bounding box, with |
| 75 | + # an offset by 0.5 cm horizontally and 0.8 cm vertically from the anchor point, and |
| 76 | + # plotted horizontally. |
| 77 | + position=Position("BL", offset=(0.5, 0.8)), |
| 78 | + orientation="horizontal", |
83 | 79 | box=True, |
84 | 80 | # Divide colorbar into equal-sized rectangles |
85 | 81 | equalsize=0.5, |
|
0 commit comments