-
Notifications
You must be signed in to change notification settings - Fork 235
Tutorial: Adding a data set on top of a topographic surface via "drapegrid" of "Figure.grdview" #3316
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
Tutorial: Adding a data set on top of a topographic surface via "drapegrid" of "Figure.grdview" #3316
Changes from 59 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
5417f38
Add two ideas for gallery example showing the 'drapegrid' parameter
yvonnefroehlich f849ced
Move and rename file
yvonnefroehlich 0669fa5
Add code block separator
yvonnefroehlich 08549a7
Add shebang
yvonnefroehlich a3b31c6
Remove shebang
yvonnefroehlich 2ea0b9a
Select thumbnail image
yvonnefroehlich 744287c
Remove 'r'
yvonnefroehlich 7799c8d
TEST: Modifiy exisiting example
yvonnefroehlich bee9125
TEST: Redo change
yvonnefroehlich ea05d25
Remove execution permission
yvonnefroehlich b43228c
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 8d5eed7
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich d74f5d8
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 193743b
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 0269127
Use rasterio and xarray to load PNG image
yvonnefroehlich 1d18eea
Remove execute permission
yvonnefroehlich 3b152c4
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 520e240
Move to tutoria advanced folder
yvonnefroehlich 3ce58e1
Restructure for tutorial
yvonnefroehlich a3b5a9e
Remove execution permission
yvonnefroehlich 77d9698
Fix bullet point list
yvonnefroehlich b09529c
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich fc25873
Remove blank line
yvonnefroehlich 9cf465a
Remove execution permission
yvonnefroehlich ea1369b
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 9c14e58
Remove seperation lines
yvonnefroehlich 3e4b55a
Set link to gmt documentation
yvonnefroehlich e960cf2
Remove variables as only once used in grid examples
yvonnefroehlich 017f320
Pass argument directly to 'perspective' parameter for grid example
yvonnefroehlich f0c114b
Determine z values from relief grid min max value
yvonnefroehlich 588c07b
Use more clearer comment for 'series' parameter
yvonnefroehlich 07dd23c
Reduce resolution
yvonnefroehlich 9976c5b
Use pandas DataFrame for cities
yvonnefroehlich 150f6f2
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 9e68390
Fix code style for DataFrame
yvonnefroehlich c59af7a
Fix code style 'values' to 'to_numpy'
yvonnefroehlich 56eea56
Fix typo in code
yvonnefroehlich e88daed
Remove execution permission
yvonnefroehlich 7441a94
Fix typos
yvonnefroehlich 965a29e
Improve comments | Subsection for tutorial
yvonnefroehlich aafda1a
Fix typos
yvonnefroehlich 8e48b87
Fix link to GMT documentation
yvonnefroehlich 3e5959e
Remove execution permission
yvonnefroehlich 194b7bc
Add explanations
yvonnefroehlich 0e83930
Remove execution permission
yvonnefroehlich cdf0e60
Adjsut thumbnail image
yvonnefroehlich 90676e9
Add highlighting
yvonnefroehlich b3da999
Improve file name of tutorial
yvonnefroehlich 1038e86
Use complete line length of 88 characters
yvonnefroehlich ce940a1
Move pandas.DataFrame for cities to plotting part
yvonnefroehlich 0bbc1bc
Remove double white spaces
yvonnefroehlich b758166
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich bc82fda
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 377ae47
Add elevation to DataFrame and use plot3d for cities
yvonnefroehlich 28f6de1
Remove execution permission
yvonnefroehlich 02ad2d7
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 488cacb
Merge branch 'main' into add-gallery-image-on-elevation
yvonnefroehlich 5f423b3
Remove code part for plotting features on top of topography
yvonnefroehlich cb3b095
Remove un-needed import of pandas
yvonnefroehlich ede2232
Add comment regarding considering the aspect ratio of the image
yvonnefroehlich 539c617
Merge branch 'main' into add-gallery-image-on-elevation
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,134 @@ | ||
| """ | ||
| Draping a dataset on top of a topographic surface | ||
| ================================================== | ||
|
|
||
| It can be visually appealing to "drape" a dataset over a topographic surface. This can | ||
| be accomplished using the ``drapegrid`` parameter of :meth:`pygmt.Figure.grdview`. | ||
|
|
||
| This tutorial consists of two examples: | ||
|
|
||
| 1. Draping a grid | ||
|
|
||
| 2. Draping an image | ||
| """ | ||
|
|
||
| # %% | ||
|
|
||
| # Load the required packages | ||
| import pygmt | ||
| import rasterio | ||
| import xarray as xr | ||
|
|
||
| # %% | ||
| # 1. Drapping a grid | ||
| # ------------------ | ||
| # | ||
| # In the first example, the seafloor crustal age is plotted with color-coding on top of | ||
| # the topographic map of an area of the Mid-Atlantic Ridge. | ||
|
|
||
| # Define study area in degrees East or North | ||
| region_2d = [-50, 0, 36, 70] # [lon_min, lon_max, lat_min, lat_max] | ||
|
|
||
| # Download elevation and crustal age grids for the study region with a resolution of 10 | ||
| # arc-minutes and load them into xarray.DataArrays | ||
| grd_relief = pygmt.datasets.load_earth_relief(resolution="10m", region=region_2d) | ||
| grd_age = pygmt.datasets.load_earth_age(resolution="10m", region=region_2d) | ||
|
|
||
| # Determine the 3-D region from the minimum and maximum values of the relief grid | ||
| region_3d = [*region_2d, grd_relief.min().to_numpy(), grd_relief.max().to_numpy()] | ||
|
|
||
| # %% | ||
| # The topographic surface is created based on the grid passed to the ``grid`` parameter | ||
| # of :meth:`pygmt.Figure.grdview`; here we use a grid of the Earth relief. To add a | ||
| # color-coding based on *another* grid we have to pass a second grid to the | ||
| # ``drapegrid`` parameter; here we use a grid of the crustal age. In this case the | ||
| # colormap specified via the ``cmap`` parameter applies to the grid passed to | ||
| # ``drapegrid``, not to ``grid``. The azimuth and elevation a the 3-D plot are set via | ||
| # the ``perspective`` parameter. | ||
|
|
||
| fig = pygmt.Figure() | ||
|
|
||
| # Set up colormap for the crustal age | ||
| pygmt.config(COLOR_NAN="lightgray") | ||
| pygmt.makecpt(cmap="batlow", series=[0, 200, 1], reverse=True, overrule_bg=True) | ||
|
|
||
| fig.grdview( | ||
| projection="M12c", # Mercator projection with a width of 12 centimeters | ||
| region=region_3d, | ||
| grid=grd_relief, # Use elevation grid for z values | ||
| drapegrid=grd_age, # Use crustal age grid for color-coding | ||
| cmap=True, # Use colormap created for the crustal age | ||
| surftype="i", # Create an image plot | ||
| # Use an illumination from the azimuthal directions 0° (north) and 270° | ||
| # (west) with a normalization via a cumulative Laplace distribution for | ||
| # the shading | ||
| shading="+a0/270+ne0.6", | ||
| perspective=[157.5, 30], # Azimuth and elevation for the 3-D plot | ||
| zsize="1.5c", | ||
| plane="+gdarkgray", | ||
| frame=True, | ||
| ) | ||
|
|
||
| # Add colorbar for the crustal age | ||
| fig.colorbar(frame=["x+lseafloor crustal age", "y+lMyr"], position="+n") | ||
|
|
||
| fig.show() | ||
|
|
||
|
|
||
| # %% | ||
| # 2. Draping an image | ||
| # ------------------- | ||
| # | ||
| # In the second example, the flag of the European Union (EU) is plotted on top of a | ||
| # topographic map of northwest Europe. This example is modified from | ||
| # :gmt-docs:`GMT example 32 </gallery/ex32.html>`. | ||
|
|
||
| # Define study area in degrees East or North | ||
| region_2d = [3, 9, 50, 54] # [lon_min, lon_max, lat_min, lat_max] | ||
|
|
||
| # Download elevation grid for the study region with a resolution of 30 arc-seconds and | ||
| # pixel registration and load it into an xarray.DataArray | ||
| grd_relief = pygmt.datasets.load_earth_relief(resolution="30s", region=region_2d) | ||
|
|
||
| # Determine the 3-D region from the minimum and maximum values of the relief grid | ||
| region_3d = [*region_2d, grd_relief.min().to_numpy(), grd_relief.max().to_numpy()] | ||
|
|
||
| # Download an PNG image of the flag of the EU using rasterio and load it into a | ||
| # xarray.DataArray | ||
| url_to_image = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b7/Flag_of_Europe.svg/1000px-Flag_of_Europe.svg.png" | ||
| with rasterio.open(url_to_image) as dataset: | ||
| data = dataset.read() | ||
| drapegrid = xr.DataArray(data, dims=("band", "y", "x")) | ||
|
|
||
| # %% | ||
| # Again we create a 3-D plot with :meth:`pygmt.Figure.grdview` and passe an Earth relief | ||
| # grid to the ``grid`` parameter to create the topographic surface. But now we pass the | ||
| # PNG image which was loaded into an :class:`xarray.DataArray` to the ``drapgrid`` | ||
| # parameter. | ||
|
|
||
| fig = pygmt.Figure() | ||
|
|
||
| # Set up a colormap with two colors for the EU flag: blue (0/51/153) for the background | ||
| # (value 0 in the nedCDF file -> lower half of 0-255 range) and yellow (255/204/0) for | ||
| # the stars (value 255 -> upper half) | ||
| pygmt.makecpt(cmap="0/51/153,255/204/0", series=[0, 256, 128]) | ||
|
|
||
| fig.grdview( | ||
| projection="M12c", # Mercator projection with a width of 12 centimeters | ||
| region=region_3d, | ||
| grid=grd_relief, # Use elevation grid for z values | ||
| drapegrid=drapegrid, # Drap image grid for the EU flag on top | ||
| cmap=True, # Use colormap defined for the EU flag | ||
| surftype="i", # Create an image plot | ||
| # Use an illumination from the azimuthal directions 0° (north) and 270° (west) with | ||
| # a normalization via a cumulative Laplace distribution for the shading | ||
| shading="+a0/270+ne0.6", | ||
| perspective=[157.5, 30], # Define azimuth, elevation for the 3-D plot | ||
| zsize="1c", | ||
| plane="+gdarkgray", | ||
| frame=True, | ||
| ) | ||
|
|
||
| fig.show() | ||
|
|
||
| # sphinx_gallery_thumbnail_number = 2 | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you forget
zsizehere?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the
zsizeparameter (temporarily), as I have not figured out yet, how to adjust the position of the following plotting elements (added viacoast,plot,text) corresponding to the value ofzsize:zsizezsize="1c"zsize="1.5c"zsize="2c"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For points,
plot3dis needed. For coastlines, it's likely we need to dump the coastlines and the usegrdtrackto determine the z-value for each data points and then pass them toplot3d.