From 3b86d2d28c9cea0e2d3ccb5696b05e252e8ce657 Mon Sep 17 00:00:00 2001 From: Liam Toney <38269494+liamtoney@users.noreply.github.com> Date: Tue, 30 Jun 2020 22:15:01 -0600 Subject: [PATCH 1/6] Add gallery example for grdview --- examples/gallery/grid/grdview_surface.py | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/gallery/grid/grdview_surface.py diff --git a/examples/gallery/grid/grdview_surface.py b/examples/gallery/grid/grdview_surface.py new file mode 100644 index 00000000000..ffc16b94849 --- /dev/null +++ b/examples/gallery/grid/grdview_surface.py @@ -0,0 +1,49 @@ +""" +Plotting a surface +------------------ + +The :func:`pygmt.grdview()` function can plot 3-D surfaces with ``surftype="s"``. Here, +we supply the data as an :class:`xarray.DataArray` with the coordinate vectors ``x`` and +``y`` defined. Note that the ``perspective`` argument here controls the azimuth and +elevation angle of the view. We provide a list of two arguments to ``frame`` — the +second argument, prepended with ``"z"``, specifies the :math:`z`-axis frame attributes. +Specifying the same scale for the ``projection`` and ``zcale`` arguments ensures equal +axis scaling. +""" + +import pygmt +import numpy as np +import xarray + +# Define an interesting function of two variables, see: +# https://en.wikipedia.org/wiki/Ackley_function +def ackley(x, y): + return ( + -20 * np.exp(-0.2 * np.sqrt(0.5 * (x ** 2 + y ** 2))) + - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) + + np.exp(1) + + 20 + ) + + +# Create gridded data +INC = 0.05 +x = np.arange(-5, 5 + INC, INC) +y = np.arange(-5, 5 + INC, INC) +data = xarray.DataArray(ackley(*np.meshgrid(x, y)), coords=(x, y)) + +fig = pygmt.Figure() + +# Plot grid as a 3-D surface +SCALE = 0.2 # [inches] +fig.grdview( + data, + frame=["a5f1", "za5f1"], + projection=f"x{SCALE}i", + zscale=f"{SCALE}i", + surftype="s", + cmap="roma", + perspective="135/30", +) + +fig.show() From 3ef7d8bf01c59aae5eeb0fa8e7cd3682570bcf15 Mon Sep 17 00:00:00 2001 From: Liam Toney <38269494+liamtoney@users.noreply.github.com> Date: Tue, 30 Jun 2020 22:29:37 -0600 Subject: [PATCH 2/6] Add second line before function definition --- examples/gallery/grid/grdview_surface.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/gallery/grid/grdview_surface.py b/examples/gallery/grid/grdview_surface.py index ffc16b94849..6d97316e6f0 100644 --- a/examples/gallery/grid/grdview_surface.py +++ b/examples/gallery/grid/grdview_surface.py @@ -15,6 +15,7 @@ import numpy as np import xarray + # Define an interesting function of two variables, see: # https://en.wikipedia.org/wiki/Ackley_function def ackley(x, y): From cfd8abfb0fef4002696152991ed4f5f5324f49c1 Mon Sep 17 00:00:00 2001 From: Liam Toney <38269494+liamtoney@users.noreply.github.com> Date: Tue, 30 Jun 2020 22:42:26 -0600 Subject: [PATCH 3/6] Fix incorrect method syntax --- examples/gallery/grid/grdview_surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/grid/grdview_surface.py b/examples/gallery/grid/grdview_surface.py index 6d97316e6f0..c7cadb65c2c 100644 --- a/examples/gallery/grid/grdview_surface.py +++ b/examples/gallery/grid/grdview_surface.py @@ -2,7 +2,7 @@ Plotting a surface ------------------ -The :func:`pygmt.grdview()` function can plot 3-D surfaces with ``surftype="s"``. Here, +The :func:`pygmt.Figure.grdview()` method can plot 3-D surfaces with ``surftype="s"``. Here, we supply the data as an :class:`xarray.DataArray` with the coordinate vectors ``x`` and ``y`` defined. Note that the ``perspective`` argument here controls the azimuth and elevation angle of the view. We provide a list of two arguments to ``frame`` — the From e70f8fb769511b3509890853e6e0edfa8eb44dc8 Mon Sep 17 00:00:00 2001 From: Liam Toney <38269494+liamtoney@users.noreply.github.com> Date: Tue, 30 Jun 2020 22:43:00 -0600 Subject: [PATCH 4/6] Use "meth" instead of "func" --- examples/gallery/grid/grdview_surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/grid/grdview_surface.py b/examples/gallery/grid/grdview_surface.py index c7cadb65c2c..1187251be7f 100644 --- a/examples/gallery/grid/grdview_surface.py +++ b/examples/gallery/grid/grdview_surface.py @@ -2,7 +2,7 @@ Plotting a surface ------------------ -The :func:`pygmt.Figure.grdview()` method can plot 3-D surfaces with ``surftype="s"``. Here, +The :meth:`pygmt.Figure.grdview()` method can plot 3-D surfaces with ``surftype="s"``. Here, we supply the data as an :class:`xarray.DataArray` with the coordinate vectors ``x`` and ``y`` defined. Note that the ``perspective`` argument here controls the azimuth and elevation angle of the view. We provide a list of two arguments to ``frame`` — the From 402899c7af55db2da258d7fe7d4bf0725e77c6a8 Mon Sep 17 00:00:00 2001 From: Liam Toney <38269494+liamtoney@users.noreply.github.com> Date: Wed, 1 Jul 2020 22:33:00 -0600 Subject: [PATCH 5/6] import xarray as xr Co-authored-by: Dongdong Tian --- examples/gallery/grid/grdview_surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/grid/grdview_surface.py b/examples/gallery/grid/grdview_surface.py index 1187251be7f..fb0a29cdc34 100644 --- a/examples/gallery/grid/grdview_surface.py +++ b/examples/gallery/grid/grdview_surface.py @@ -13,7 +13,7 @@ import pygmt import numpy as np -import xarray +import xarray as xr # Define an interesting function of two variables, see: From 3b2392eeaa04f484c77540e86537b3ddb8b6d719 Mon Sep 17 00:00:00 2001 From: Liam Toney <38269494+liamtoney@users.noreply.github.com> Date: Wed, 1 Jul 2020 22:33:44 -0600 Subject: [PATCH 6/6] Finish updating to use xr --- examples/gallery/grid/grdview_surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/grid/grdview_surface.py b/examples/gallery/grid/grdview_surface.py index fb0a29cdc34..01ec7f08746 100644 --- a/examples/gallery/grid/grdview_surface.py +++ b/examples/gallery/grid/grdview_surface.py @@ -31,7 +31,7 @@ def ackley(x, y): INC = 0.05 x = np.arange(-5, 5 + INC, INC) y = np.arange(-5, 5 + INC, INC) -data = xarray.DataArray(ackley(*np.meshgrid(x, y)), coords=(x, y)) +data = xr.DataArray(ackley(*np.meshgrid(x, y)), coords=(x, y)) fig = pygmt.Figure()