|
| 1 | +""" |
| 2 | +Quoted lines |
| 3 | +------------ |
| 4 | +To plot a so-called *quoted line*, i.e., labels along a line |
| 5 | +or curve, use the ``style`` parameter of the |
| 6 | +:meth:`pygmt.Figure.plot` method with the argument ``"q"`` and the |
| 7 | +desired modifiers. A colon (``":"``) is used to separate the |
| 8 | +algorithm settings from the label information. |
| 9 | +This example shows how to adjust the labels. |
| 10 | +For modifying the baseline via the ``pen`` parameter, see the |
| 11 | +:doc:`Line styles example </gallery/lines/linestyles>`. |
| 12 | +For details on the input data see the upstream GMT documentation |
| 13 | +at https://docs.generic-mapping-tools.org/latest/plot.html#s. |
| 14 | +""" |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +import pygmt |
| 18 | + |
| 19 | +# Generate a two-point line for plotting |
| 20 | +x = np.array([1, 4]) |
| 21 | +y = np.array([20, 20]) |
| 22 | + |
| 23 | +fig = pygmt.Figure() |
| 24 | +fig.basemap(region=[0, 10, 0, 20], projection="X15c/15c", frame="+tQuoted Lines") |
| 25 | + |
| 26 | +# Plot different quoted lines |
| 27 | +for quotedline in [ |
| 28 | + # Line with labels ("+l") "text" in distance ("d") of 1 centimeter |
| 29 | + "qd1c:+ltext", |
| 30 | + # Suppress baseline by appending "+i" |
| 31 | + "qd1c:+ltext+i", |
| 32 | + # Give the number of equally spaced labels by using "n" instead of "d" |
| 33 | + "qn5:+ltext", |
| 34 | + # Use upper-case "N" to have labels at the start and end of the line |
| 35 | + "qN5:+ltext", |
| 36 | + # To only plot a label at the start of the line use "N-1" |
| 37 | + "qN-1:+ltext", |
| 38 | + # To only plot a label at the end of the line use "N+1" |
| 39 | + "qN+1:+ltext", |
| 40 | + # Adjust the justification of the labels via "+j", here Top Center |
| 41 | + "qd1c:+ltext+jTC", |
| 42 | + # Shift labels using "+n" in x and y directions relative to the baseline |
| 43 | + "qd1c:+ltext+n-0.5c/0.1c", |
| 44 | + # Rotate labels via "+a" (counter-clockwise from horizontal) |
| 45 | + "qd1c:+ltext+a20", |
| 46 | + # Adjust size, type, and color of the font via "+f" |
| 47 | + "qd1c:+ltext+f12p,Times-Bold,red", |
| 48 | + # Add a box around the label via "+p" |
| 49 | + "qd1c:+ltext+p", |
| 50 | + # Adjust thickness, color, and style of the outline |
| 51 | + "qd1c:+ltext+p0.5p,blue,dashed", |
| 52 | + # Append "+o" to get a box with rounded edges |
| 53 | + "qd1c:+ltext+p0.5p,blue+o", |
| 54 | + # Adjust the space between label and box in x and y directions via "+c" |
| 55 | + "qd1c:+ltext+p0.5p,blue+o+c0.1c/0.1c", |
| 56 | + # Give a fill of the box via "+g" together with the desired color |
| 57 | + "qd1c:+ltext+gdodgerblue", |
| 58 | +]: |
| 59 | + y -= 1 # Move current line down |
| 60 | + fig.plot(x=x, y=y, pen="1.25p", style=quotedline) |
| 61 | + fig.text( |
| 62 | + x=x[-1], |
| 63 | + y=y[-1], |
| 64 | + text=quotedline, |
| 65 | + font="Courier-Bold", |
| 66 | + justify="ML", |
| 67 | + offset="0.75c/0c", |
| 68 | + ) |
| 69 | + |
| 70 | +fig.show() |
| 71 | + |
| 72 | + |
| 73 | +############################################################################### |
| 74 | +# For curved labels following the line, append ``"+v"`` to the argument passed |
| 75 | +# to the ``style`` parameter. |
| 76 | + |
| 77 | +# Generate sinus curve |
| 78 | +x = np.arange(0, 10 * np.pi, 0.1) |
| 79 | +y = np.sin(0.8 * x) |
| 80 | + |
| 81 | +fig = pygmt.Figure() |
| 82 | + |
| 83 | +fig.basemap(region=[0, 30, -4, 4], projection="X10c/5c", frame=True) |
| 84 | + |
| 85 | +fig.plot(x=x, y=y + 2, style="qd1.2c:+lstraight text+f5p", pen="1p,blue") |
| 86 | + |
| 87 | +fig.plot( |
| 88 | + x=x, |
| 89 | + y=y - 2, |
| 90 | + # Append "+v" to force curved labels |
| 91 | + style="qd1.2c:+lcurved text+f5p+v", |
| 92 | + pen="1p,blue", |
| 93 | +) |
| 94 | + |
| 95 | +fig.show() |
0 commit comments