Skip to content

Commit

Permalink
Expand table-like input options for Figure.contour (#1531)
Browse files Browse the repository at this point in the history
* Expand table-like input options for Figure.contour
* Test pandas, numpy, xarray input in test_contour.py
  • Loading branch information
maxrjones committed Sep 22, 2021
1 parent cbe44aa commit ef108d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
22 changes: 8 additions & 14 deletions pygmt/src/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
from pygmt.clib import Session
from pygmt.helpers import (
build_arg_string,
data_kind,
deprecate_parameter,
dummy_context,
fmt_docstring,
kwargs_to_strings,
use_alias,
Expand Down Expand Up @@ -60,8 +58,10 @@ def contour(self, x=None, y=None, z=None, data=None, **kwargs):
----------
x/y/z : 1d arrays
Arrays of x and y coordinates and values z of the data points.
data : str or 2d array
Either a data file name or a 2d numpy array with the tabular data.
data : str or {table-like}
Pass in (x, y, z) or (longitude, latitude, elevation) values by
providing a file name to an ASCII data table, a 2D
{table-classes}
{J}
{R}
annotation : str or int
Expand Down Expand Up @@ -126,17 +126,11 @@ def contour(self, x=None, y=None, z=None, data=None, **kwargs):
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access

kind = data_kind(data, x, y, z, required_z=True)

with Session() as lib:
# Choose how data will be passed in to the module
if kind == "file":
file_context = dummy_context(data)
elif kind == "matrix":
file_context = lib.virtualfile_from_matrix(data)
elif kind == "vectors":
file_context = lib.virtualfile_from_vectors(x, y, z)

# Choose how data will be passed into the module
file_context = lib.virtualfile_from_data(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
)
with file_context as fname:
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("contour", arg_str)
16 changes: 12 additions & 4 deletions pygmt/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import os

import numpy as np
import pandas as pd
import pytest
import xarray as xr
from pygmt import Figure

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
Expand All @@ -17,7 +19,7 @@ def data():
"""
Load the point data from the test file.
"""
return np.loadtxt(POINTS_DATA)
return pd.read_table(POINTS_DATA, header=None, sep=r"\s+")


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -45,13 +47,19 @@ def test_contour_vec(region):
return fig


@pytest.mark.mpl_image_compare
def test_contour_matrix(data, region):
@pytest.mark.mpl_image_compare(filename="test_contour_matrix.png")
@pytest.mark.parametrize(
"array_func",
[np.array, pd.DataFrame, xr.Dataset],
)
def test_contour_matrix(array_func, data, region):
"""
Plot data.
"""
fig = Figure()
fig.contour(data=data, projection="X10c", region=region, frame="ag", pen=True)
fig.contour(
data=array_func(data), projection="X10c", region=region, frame="ag", pen=True
)
return fig


Expand Down

0 comments on commit ef108d9

Please sign in to comment.