-
Notifications
You must be signed in to change notification settings - Fork 228
DOC: Add basic tutorial "Plotting polygons" #3593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
bfdbb35
Add basci tutorial for polygons - code
yvonnefroehlich 2f2d2f7
Add documentation
yvonnefroehlich 8a67033
Merge branch 'main' into add-tut-polygons
yvonnefroehlich 9b53169
Merge branch 'main' into add-tut-polygons
yvonnefroehlich 7048919
Add link to gallery example
yvonnefroehlich 7a8932d
Correct "xshift" argument
yvonnefroehlich cf8ee7f
Write 'region' argument as long list
yvonnefroehlich bd71d3e
Use only NumPy arrays
yvonnefroehlich 2e5a7e2
Fix typos
yvonnefroehlich a299405
Correct link
yvonnefroehlich ce35b66
Add highlighting
yvonnefroehlich 994b93e
Shorten formulation and add link
yvonnefroehlich e537e27
Merge branch 'main' into add-tut-polygons
yvonnefroehlich fef82cf
Adjust line length
yvonnefroehlich 9dd1718
Merge branch 'main' into add-tut-polygons
yvonnefroehlich 7dea331
Rewrap to 88 chars
yvonnefroehlich 70db43a
Remove blank lines
yvonnefroehlich 28a37e5
Remove blank lines
yvonnefroehlich 1ae9445
Add definition for 'polygon'
yvonnefroehlich 7463a82
Mention default change when 'fill' is used
yvonnefroehlich cbcb1c2
Re-wrapp to 88 chars
yvonnefroehlich 982bb83
Adjust thumbnail_number
yvonnefroehlich 2a7cf63
Write hypen-separated word in one line to avoid white space the two p…
yvonnefroehlich 835d113
Merge branch 'main' into add-tut-polygons
yvonnefroehlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
""" | ||
Plotting polygons | ||
================= | ||
|
||
Plotting polygons is handled by the :meth:`pygmt.Figure.plot` method. | ||
|
||
This tutorial focuses on input data given as NumPy arrays. Besides NumPy arrays, | ||
array-like objects are supported. Here, a polygon is a closed shape defined by a series | ||
of data points with x and y coordinates, connected by line segments, with the start and | ||
end points being identical. For plotting a :class:`geopandas.GeoDataFrame` object with | ||
polygon geometries, e.g., to create a choropleth map, see the gallery example | ||
:doc:`Choropleth map </gallery/maps/choropleth_map>`. | ||
""" | ||
|
||
# %% | ||
import numpy as np | ||
import pygmt | ||
|
||
# %% | ||
# Plot polygons | ||
# ------------- | ||
# | ||
# Set up sample data points as NumPy arrays for the x and y values. | ||
|
||
x = np.array([-2, 1, 3, 0, -4, -2]) | ||
y = np.array([-3, -1, 1, 3, 2, -3]) | ||
|
||
# %% | ||
# Create a Cartesian plot via the :meth:`pygmt.Figure.basemap` method. Pass arrays to | ||
# the ``x`` and ``y`` parameters of the :meth:`pygmt.Figure.plot` method. Without | ||
# further adjustments, lines are drawn between the data points. By default, the lines | ||
# are 0.25-points thick, black, and solid. In this example, the data points are chosen | ||
# to make the lines form a polygon. | ||
|
||
fig = pygmt.Figure() | ||
fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
fig.plot(x=x, y=y) | ||
fig.show() | ||
|
||
# %% | ||
# The ``pen`` parameter can be used to adjust the lines or outline of the polygon. The | ||
# argument passed to ``pen`` is one string with the comma-separated optional values | ||
# *width*,\ *color*,\ *style*. | ||
|
||
fig = pygmt.Figure() | ||
fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
# Use a 2-points thick, darkred, dashed outline | ||
fig.plot(x=x, y=y, pen="2p,darkred,dashed") | ||
fig.show() | ||
|
||
# %% | ||
# Use the ``fill`` parameter to fill the polygon with a color or | ||
# :doc:`pattern </techref/patterns>`. Note, that there are no lines drawn between the | ||
# data points by default if ``fill`` is used. Use the ``pen`` parameter to add an | ||
# outline around the polygon. | ||
|
||
fig = pygmt.Figure() | ||
fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
# Fill the polygon with color "orange" | ||
fig.plot(x=x, y=y, fill="orange") | ||
fig.show() | ||
|
||
|
||
# %% | ||
# Close polygons | ||
# -------------- | ||
# | ||
# Set up sample data points as NumPy array for the x and y values. Now, the data points | ||
# do not form a polygon. | ||
|
||
x = np.array([-2, 1, 3, 0, -4]) | ||
y = np.array([-3, -1, 1, 3, 2]) | ||
|
||
# %% | ||
# The ``close`` parameter can be used to force the polygon to be closed. | ||
|
||
fig = pygmt.Figure() | ||
fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
fig.plot(x=x, y=y, pen=True) | ||
|
||
fig.shift_origin(xshift="w+1c") | ||
|
||
fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
fig.plot(x=x, y=y, pen=True, close=True) | ||
fig.show() | ||
|
||
# %% | ||
# When using the ``fill`` parameter, the polygon is automatically closed. | ||
|
||
fig = pygmt.Figure() | ||
fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
fig.plot(x=x, y=y, pen=True) | ||
|
||
fig.shift_origin(xshift="w+1c") | ||
|
||
fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
fig.plot(x=x, y=y, pen=True, fill="orange") | ||
fig.show() | ||
|
||
# sphinx_gallery_thumbnail_number = 5 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.