Skip to content

Commit ef108d9

Browse files
author
Meghan Jones
authored
Expand table-like input options for Figure.contour (#1531)
* Expand table-like input options for Figure.contour * Test pandas, numpy, xarray input in test_contour.py
1 parent cbe44aa commit ef108d9

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

pygmt/src/contour.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
from pygmt.clib import Session
66
from pygmt.helpers import (
77
build_arg_string,
8-
data_kind,
98
deprecate_parameter,
10-
dummy_context,
119
fmt_docstring,
1210
kwargs_to_strings,
1311
use_alias,
@@ -60,8 +58,10 @@ def contour(self, x=None, y=None, z=None, data=None, **kwargs):
6058
----------
6159
x/y/z : 1d arrays
6260
Arrays of x and y coordinates and values z of the data points.
63-
data : str or 2d array
64-
Either a data file name or a 2d numpy array with the tabular data.
61+
data : str or {table-like}
62+
Pass in (x, y, z) or (longitude, latitude, elevation) values by
63+
providing a file name to an ASCII data table, a 2D
64+
{table-classes}
6565
{J}
6666
{R}
6767
annotation : str or int
@@ -126,17 +126,11 @@ def contour(self, x=None, y=None, z=None, data=None, **kwargs):
126126
"""
127127
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
128128

129-
kind = data_kind(data, x, y, z, required_z=True)
130-
131129
with Session() as lib:
132-
# Choose how data will be passed in to the module
133-
if kind == "file":
134-
file_context = dummy_context(data)
135-
elif kind == "matrix":
136-
file_context = lib.virtualfile_from_matrix(data)
137-
elif kind == "vectors":
138-
file_context = lib.virtualfile_from_vectors(x, y, z)
139-
130+
# Choose how data will be passed into the module
131+
file_context = lib.virtualfile_from_data(
132+
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
133+
)
140134
with file_context as fname:
141135
arg_str = " ".join([fname, build_arg_string(kwargs)])
142136
lib.call_module("contour", arg_str)

pygmt/tests/test_contour.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import os
66

77
import numpy as np
8+
import pandas as pd
89
import pytest
10+
import xarray as xr
911
from pygmt import Figure
1012

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

2224

2325
@pytest.fixture(scope="module")
@@ -45,13 +47,19 @@ def test_contour_vec(region):
4547
return fig
4648

4749

48-
@pytest.mark.mpl_image_compare
49-
def test_contour_matrix(data, region):
50+
@pytest.mark.mpl_image_compare(filename="test_contour_matrix.png")
51+
@pytest.mark.parametrize(
52+
"array_func",
53+
[np.array, pd.DataFrame, xr.Dataset],
54+
)
55+
def test_contour_matrix(array_func, data, region):
5056
"""
5157
Plot data.
5258
"""
5359
fig = Figure()
54-
fig.contour(data=data, projection="X10c", region=region, frame="ag", pen=True)
60+
fig.contour(
61+
data=array_func(data), projection="X10c", region=region, frame="ag", pen=True
62+
)
5563
return fig
5664

5765

0 commit comments

Comments
 (0)