|
| 1 | +""" |
| 2 | +Double Y axes graph |
| 3 | +------------------- |
| 4 | +
|
| 5 | +The ``frame`` parameter of the plotting methods of the :class:`pygmt.Figure` |
| 6 | +class can control which axes should be plotted and optionally show annotations, |
| 7 | +tick marks, and gridlines. By default, all 4 axes are plotted, along with annotations |
| 8 | +and tick marks (denoted **W**, **S**, **E**, **N**). Lower case versions (**w**, **s**, |
| 9 | +**e**, **n**) can be used to denote to only plot the axes with tick marks. We |
| 10 | +can also only plot the axes without annotations and tick marks using **l** |
| 11 | +(left axis), **r** (right axis), **t** (top axis), **b** (bottom axis). |
| 12 | +When ``frame`` is used to change the frame settings, any axes |
| 13 | +that are not defined using one of these three options are not |
| 14 | +drawn. |
| 15 | +
|
| 16 | +To plot a double Y-axes graph using PyGMT, we need to plot at least two base maps |
| 17 | +separately. The base maps should share the same projection parameter and |
| 18 | +x-axis limits, but different y-axis limits. |
| 19 | +""" |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import pygmt |
| 23 | + |
| 24 | +# Generate two sample Y data from one common X data |
| 25 | +x = np.linspace(1.0, 9.0, num=9) |
| 26 | +y1 = x |
| 27 | +y2 = x ** 2 + 110 |
| 28 | + |
| 29 | +fig = pygmt.Figure() |
| 30 | + |
| 31 | +# Plot the common X axes |
| 32 | +# The bottom axis (S) is plotted with annotations and tick marks |
| 33 | +# The top axis (t) is plotted without annotations and tick marks |
| 34 | +# The left and right axes are not drawn |
| 35 | +fig.basemap(region=[0, 10, 0, 10], projection="X15c/15c", frame=["St", "xaf+lx"]) |
| 36 | + |
| 37 | +# Plot the Y axis for y1 data |
| 38 | +# The left axis (W) is plotted with blue annotations, ticks, and label |
| 39 | +with pygmt.config( |
| 40 | + MAP_FRAME_PEN="blue", |
| 41 | + MAP_TICK_PEN="blue", |
| 42 | + FONT_ANNOT_PRIMARY="blue", |
| 43 | + FONT_LABEL="blue", |
| 44 | +): |
| 45 | + fig.basemap(frame=["W", "yaf+ly1"]) |
| 46 | + |
| 47 | +# Plot the line for y1 data |
| 48 | +fig.plot(x=x, y=y1, pen="1p,blue") |
| 49 | +# Plot points for y1 data |
| 50 | +fig.plot(x=x, y=y1, style="c0.2c", color="blue", label="y1") |
| 51 | + |
| 52 | +# Plot the Y axis for y2 data |
| 53 | +# The right axis (E) is plotted with red annotations, ticks, and label |
| 54 | +with pygmt.config( |
| 55 | + MAP_FRAME_PEN="red", |
| 56 | + MAP_TICK_PEN="red", |
| 57 | + FONT_ANNOT_PRIMARY="red", |
| 58 | + FONT_LABEL="red", |
| 59 | +): |
| 60 | + fig.basemap(region=[0, 10, 100, 200], frame=["E", "yaf+ly2"]) |
| 61 | +# Plot the line for y2 data |
| 62 | +fig.plot(x=x, y=y2, pen="1p,red") |
| 63 | +# Plot points for y2 data |
| 64 | +fig.plot(x=x, y=y2, style="s0.28c", color="red", label="y2") |
| 65 | + |
| 66 | +# Create a legend in the top-left corner of the plot |
| 67 | +fig.legend(position="jTL+o0.1c", box=True) |
| 68 | + |
| 69 | +fig.show() |
0 commit comments