|
| 1 | +""" |
| 2 | +Plotting polygons |
| 3 | +================= |
| 4 | +
|
| 5 | +Plotting polygons is handled by the :meth:`pygmt.Figure.plot` method. |
| 6 | +
|
| 7 | +This tutorial focuses on input data given as NumPy arrays. Besides NumPy arrays, |
| 8 | +array-like objects are supported. Here, a polygon is a closed shape defined by a series |
| 9 | +of data points with x and y coordinates, connected by line segments, with the start and |
| 10 | +end points being identical. For plotting a :class:`geopandas.GeoDataFrame` object with |
| 11 | +polygon geometries, e.g., to create a choropleth map, see the gallery example |
| 12 | +:doc:`Choropleth map </gallery/maps/choropleth_map>`. |
| 13 | +""" |
| 14 | + |
| 15 | +# %% |
| 16 | +import numpy as np |
| 17 | +import pygmt |
| 18 | + |
| 19 | +# %% |
| 20 | +# Plot polygons |
| 21 | +# ------------- |
| 22 | +# |
| 23 | +# Set up sample data points as NumPy arrays for the x and y values. |
| 24 | + |
| 25 | +x = np.array([-2, 1, 3, 0, -4, -2]) |
| 26 | +y = np.array([-3, -1, 1, 3, 2, -3]) |
| 27 | + |
| 28 | +# %% |
| 29 | +# Create a Cartesian plot via the :meth:`pygmt.Figure.basemap` method. Pass arrays to |
| 30 | +# the ``x`` and ``y`` parameters of the :meth:`pygmt.Figure.plot` method. Without |
| 31 | +# further adjustments, lines are drawn between the data points. By default, the lines |
| 32 | +# are 0.25-points thick, black, and solid. In this example, the data points are chosen |
| 33 | +# to make the lines form a polygon. |
| 34 | + |
| 35 | +fig = pygmt.Figure() |
| 36 | +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) |
| 37 | +fig.plot(x=x, y=y) |
| 38 | +fig.show() |
| 39 | + |
| 40 | +# %% |
| 41 | +# The ``pen`` parameter can be used to adjust the lines or outline of the polygon. The |
| 42 | +# argument passed to ``pen`` is one string with the comma-separated optional values |
| 43 | +# *width*,\ *color*,\ *style*. |
| 44 | + |
| 45 | +fig = pygmt.Figure() |
| 46 | +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) |
| 47 | +# Use a 2-points thick, darkred, dashed outline |
| 48 | +fig.plot(x=x, y=y, pen="2p,darkred,dashed") |
| 49 | +fig.show() |
| 50 | + |
| 51 | +# %% |
| 52 | +# Use the ``fill`` parameter to fill the polygon with a color or |
| 53 | +# :doc:`pattern </techref/patterns>`. Note, that there are no lines drawn between the |
| 54 | +# data points by default if ``fill`` is used. Use the ``pen`` parameter to add an |
| 55 | +# outline around the polygon. |
| 56 | + |
| 57 | +fig = pygmt.Figure() |
| 58 | +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) |
| 59 | +# Fill the polygon with color "orange" |
| 60 | +fig.plot(x=x, y=y, fill="orange") |
| 61 | +fig.show() |
| 62 | + |
| 63 | + |
| 64 | +# %% |
| 65 | +# Close polygons |
| 66 | +# -------------- |
| 67 | +# |
| 68 | +# Set up sample data points as NumPy array for the x and y values. Now, the data points |
| 69 | +# do not form a polygon. |
| 70 | + |
| 71 | +x = np.array([-2, 1, 3, 0, -4]) |
| 72 | +y = np.array([-3, -1, 1, 3, 2]) |
| 73 | + |
| 74 | +# %% |
| 75 | +# The ``close`` parameter can be used to force the polygon to be closed. |
| 76 | + |
| 77 | +fig = pygmt.Figure() |
| 78 | +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) |
| 79 | +fig.plot(x=x, y=y, pen=True) |
| 80 | + |
| 81 | +fig.shift_origin(xshift="w+1c") |
| 82 | + |
| 83 | +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) |
| 84 | +fig.plot(x=x, y=y, pen=True, close=True) |
| 85 | +fig.show() |
| 86 | + |
| 87 | +# %% |
| 88 | +# When using the ``fill`` parameter, the polygon is automatically closed. |
| 89 | + |
| 90 | +fig = pygmt.Figure() |
| 91 | +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) |
| 92 | +fig.plot(x=x, y=y, pen=True) |
| 93 | + |
| 94 | +fig.shift_origin(xshift="w+1c") |
| 95 | + |
| 96 | +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) |
| 97 | +fig.plot(x=x, y=y, pen=True, fill="orange") |
| 98 | +fig.show() |
| 99 | + |
| 100 | +# sphinx_gallery_thumbnail_number = 5 |
0 commit comments