Skip to content
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

Add tutorial section on passing xr.Dataset with 1-D data_vars as input #3147

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/get_started/04_table_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import numpy as np
import pandas as pd
import pygmt
import xarray as xr

# %%
# ASCII table file
Expand Down Expand Up @@ -110,6 +111,39 @@
fig.plot(data=gdf, style="c0.2c", fill="purple")
fig.show()

# %%
# :class:`xarray.Dataset` with 1-D data variables
# -----------------------------------------------
#
# For slightly advanced users that have tabular-like data stored in an
# :class:`xarray.Dataset` object, it is also possible to pass this into the ``data``
# parameter, provided that:
#
# 1. The :class:`xarray.Dataset` input is made up of 1-D :class:`xarray.DataArray` data
# variables.
# 2. The data you want to plot or process is stored under ``data_vars``, and not under
# ``coords`` in the :class:`xarray.Dataset` data structure. Use
# :meth:`xarray.Dataset.reset_coords` if you need to convert a coordinate into a
# data variable.
#
# This is useful if you are working with data stored in file formats like HDF5 which can
# be read using :func:`xarray.open_dataset`, but not ``pandas``

# Example xr.Dataset
index = np.arange(start=0, stop=10)
distance = np.linspace(start=20, stop=50, num=10)
elevation = np.logspace(start=1, stop=-3, num=10)

ds = xr.Dataset(
data_vars={"distance": (["index"], distance), "y": (["index"], elevation)}
)
ds # noqa: B018

# %%
fig = pygmt.Figure()
fig.plot(data=ds, frame=["xaf+lDistance", "yaf+lElevation"])
fig.show()

# %%
# Scalar values or 1-D arrays
# ---------------------------
Expand Down