|
| 1 | +""" |
| 2 | +Creating a map with contour lines |
| 3 | +================================= |
| 4 | +
|
| 5 | +Plotting a contour map is handled by :meth:`pygmt.Figure.grdcontour`. |
| 6 | +""" |
| 7 | + |
| 8 | +import pygmt |
| 9 | + |
| 10 | +# Load sample earth relief data |
| 11 | +grid = pygmt.datasets.load_earth_relief(resolution="05m", region=[-92.5, -82.5, -3, 7]) |
| 12 | + |
| 13 | +######################################################################################## |
| 14 | +# Create contour plot |
| 15 | +# ------------------- |
| 16 | +# |
| 17 | +# The :meth:`pygmt.Figure.grdcontour` method takes the grid input. |
| 18 | +# It plots annotated contour lines, which are thicker and have the |
| 19 | +# elevation/depth written on them, and unannotated contour lines. |
| 20 | +# In the example below, the default contour line intervals are 500 meters, |
| 21 | +# with an annotated contour line every 1000 meters. |
| 22 | +# By default, it plots the map with the |
| 23 | +# equidistant cylindrical projection and with no frame. |
| 24 | + |
| 25 | +fig = pygmt.Figure() |
| 26 | +fig.grdcontour(grid=grid) |
| 27 | +fig.show() |
| 28 | + |
| 29 | +######################################################################################## |
| 30 | +# Contour line settings |
| 31 | +# --------------------- |
| 32 | +# |
| 33 | +# Use the ``annotation`` and ``interval`` arguments to adjust contour line intervals. |
| 34 | +# In the example below, there are contour intervals every 250 meters and |
| 35 | +# annotated contour lines every 1,000 meters. |
| 36 | + |
| 37 | +fig = pygmt.Figure() |
| 38 | +fig.grdcontour( |
| 39 | + annotation=1000, |
| 40 | + interval=250, |
| 41 | + grid=grid, |
| 42 | +) |
| 43 | +fig.show() |
| 44 | + |
| 45 | +######################################################################################## |
| 46 | +# Contour limits |
| 47 | +# -------------- |
| 48 | +# |
| 49 | +# The ``limit`` argument sets the minimum and maximum values for the contour lines. |
| 50 | +# The argument takes the low and high values, |
| 51 | +# and is either a list (as below) or a string ``limit="-4000/-2000"``. |
| 52 | + |
| 53 | +fig = pygmt.Figure() |
| 54 | +fig.grdcontour( |
| 55 | + annotation=1000, |
| 56 | + interval=250, |
| 57 | + grid=grid, |
| 58 | + limit=[-4000, -2000], |
| 59 | +) |
| 60 | +fig.show() |
| 61 | + |
| 62 | +######################################################################################## |
| 63 | +# Map settings |
| 64 | +# ------------ |
| 65 | +# |
| 66 | +# The :meth:`pygmt.Figure.grdcontour` method accepts additional arguments, |
| 67 | +# including setting the projection and frame. |
| 68 | + |
| 69 | +fig = pygmt.Figure() |
| 70 | +fig.grdcontour( |
| 71 | + annotation=1000, |
| 72 | + interval=250, |
| 73 | + grid=grid, |
| 74 | + limit=[-4000, -2000], |
| 75 | + projection="M4i", |
| 76 | + frame=True, |
| 77 | +) |
| 78 | +fig.show() |
| 79 | + |
| 80 | +######################################################################################## |
| 81 | +# Adding a colormap |
| 82 | +# ----------------- |
| 83 | +# |
| 84 | +# The :meth:`pygmt.Figure.grdimage` method can be used to add a |
| 85 | +# colormap to the contour map. It must be called prior to |
| 86 | +# :meth:`pygmt.Figure.grdcontour` to keep the contour lines visible on the final map. |
| 87 | +# If the ``projection`` argument is specified in the :meth:`pygmt.Figure.grdimage` |
| 88 | +# method, it does not need to be repeated in |
| 89 | +# the :meth:`pygmt.Figure.grdcontour` method. |
| 90 | + |
| 91 | +fig = pygmt.Figure() |
| 92 | +fig.grdimage( |
| 93 | + grid=grid, |
| 94 | + cmap="haxby", |
| 95 | + projection="M4i", |
| 96 | + frame=True, |
| 97 | +) |
| 98 | +fig.grdcontour( |
| 99 | + annotation=1000, |
| 100 | + interval=250, |
| 101 | + grid=grid, |
| 102 | + limit=[-4000, -2000], |
| 103 | +) |
| 104 | +fig.show() |
0 commit comments