From fac4d1332ba4d9e8f4a97eeb1b5843b929fe08ea Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 2 May 2021 21:46:00 -0400 Subject: [PATCH 1/6] Rename sizes to size in Figure.plot() --- pygmt/src/plot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index f3be49b598b..07454ba4878 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -43,7 +43,7 @@ t="transparency", ) @kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence") -def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): +def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs): r""" Plot lines, polygons, and symbols in 2-D. @@ -78,7 +78,7 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): Either a data file name or a 2d numpy array with the tabular data. Use parameter ``columns`` to choose which columns are x, y, color, and size, respectively. - sizes : 1d array + size : 1d array The sizes of the data points in units specified using ``style``. Only valid if using ``x``/``y``. direction : list of two 1d arrays @@ -215,12 +215,12 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): ) extra_arrays.append(kwargs["G"]) del kwargs["G"] - if sizes is not None: + if size is not None: if kind != "vectors": raise GMTInvalidInput( "Can't use arrays for sizes if data is matrix or file." ) - extra_arrays.append(sizes) + extra_arrays.append(size) for flag in ["I", "t"]: if flag in kwargs and is_nonstr_iter(kwargs[flag]): From fd122219d4301cb27cc019c3870778d18f6f77c7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 2 May 2021 21:46:35 -0400 Subject: [PATCH 2/6] Updates tests and examples to use the new parameter name --- examples/gallery/symbols/points_categorical.py | 2 +- examples/gallery/symbols/scatter.py | 4 ++-- examples/tutorials/plot.py | 6 +++--- pygmt/tests/test_plot.py | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/gallery/symbols/points_categorical.py b/examples/gallery/symbols/points_categorical.py index 9a2a302939a..3abf829e7ff 100644 --- a/examples/gallery/symbols/points_categorical.py +++ b/examples/gallery/symbols/points_categorical.py @@ -53,7 +53,7 @@ x=df.bill_length_mm, y=df.bill_depth_mm, # Vary each symbol size according to another feature (body mass, scaled by 7.5*10e-5) - sizes=df.body_mass_g * 7.5e-5, + size=df.body_mass_g * 7.5e-5, # Points colored by categorical number code color=df.species.cat.codes.astype(int), # Use colormap created by makecpt diff --git a/examples/gallery/symbols/scatter.py b/examples/gallery/symbols/scatter.py index 1631c306a9f..2009b4945fc 100644 --- a/examples/gallery/symbols/scatter.py +++ b/examples/gallery/symbols/scatter.py @@ -23,13 +23,13 @@ ) for color in ["gray73", "darkorange", "slateblue"]: x, y = np.random.rand(2, n) # random X and Y data in [0,1] - sizes = np.random.rand(n) * 0.5 # random size [0,0.5], in cm + size = np.random.rand(n) * 0.5 # random size [0,0.5], in cm # plot data points as circles (style="c"), with different sizes fig.plot( x=x, y=y, style="c", - sizes=sizes, + size=size, color=color, # Set the legend label, # and set the symbol size to be 0.25 cm (+S0.25c) in legend diff --git a/examples/tutorials/plot.py b/examples/tutorials/plot.py index 31d6d540807..cf3a0de1c42 100644 --- a/examples/tutorials/plot.py +++ b/examples/tutorials/plot.py @@ -45,7 +45,7 @@ # parameter controls the outline of the symbols and the ``color`` parameter controls the fill. # # We can map the size of the circles to the earthquake magnitude by passing an array to -# the ``sizes`` parameter. Because the magnitude is on a logarithmic scale, it helps to +# the ``size`` parameter. Because the magnitude is on a logarithmic scale, it helps to # show the differences by scaling the values using a power law. fig = pygmt.Figure() @@ -54,7 +54,7 @@ fig.plot( x=data.longitude, y=data.latitude, - sizes=0.02 * (2 ** data.magnitude), + size=0.02 * (2 ** data.magnitude), style="cc", color="white", pen="black", @@ -82,7 +82,7 @@ fig.plot( x=data.longitude, y=data.latitude, - sizes=0.02 * 2 ** data.magnitude, + size=0.02 * 2 ** data.magnitude, color=data.depth_km, cmap=True, style="cc", diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 7f50fc75219..d68c0f19249 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -93,7 +93,7 @@ def test_plot_fail_no_data(data): def test_plot_fail_color_size_intensity(data): """ - Should raise an exception if array color, sizes and intensity are used with + Should raise an exception if array color, size and intensity are used with matrix. """ fig = Figure() @@ -101,7 +101,7 @@ def test_plot_fail_color_size_intensity(data): with pytest.raises(GMTInvalidInput): fig.plot(style="c0.2c", color=data[:, 2], **kwargs) with pytest.raises(GMTInvalidInput): - fig.plot(style="cc", sizes=data[:, 2], color="red", **kwargs) + fig.plot(style="cc", size=data[:, 2], color="red", **kwargs) with pytest.raises(GMTInvalidInput): fig.plot(style="c0.2c", color="red", intensity=data[:, 2], **kwargs) @@ -152,7 +152,7 @@ def test_plot_sizes(data, region): fig.plot( x=data[:, 0], y=data[:, 1], - sizes=0.5 * data[:, 2], + size=0.5 * data[:, 2], region=region, projection="X10c", style="cc", @@ -172,7 +172,7 @@ def test_plot_colors_sizes(data, region): x=data[:, 0], y=data[:, 1], color=data[:, 2], - sizes=0.5 * data[:, 2], + size=0.5 * data[:, 2], region=region, projection="X10c", style="cc", @@ -193,7 +193,7 @@ def test_plot_colors_sizes_proj(data, region): x=data[:, 0], y=data[:, 1], color=data[:, 2], - sizes=0.5 * data[:, 2], + size=0.5 * data[:, 2], style="cc", cmap="copper", ) @@ -288,7 +288,7 @@ def test_plot_sizes_colors_transparencies(): frame=True, style="cc", color=color, - sizes=size, + size=size, cmap="gray", transparency=transparency, ) From a91c71b9235e2bbc7a1396408e3706f1a5b618b6 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 2 May 2021 21:48:01 -0400 Subject: [PATCH 3/6] Use the deprecate_parameter decorator for backward-compatibility --- pygmt/src/plot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 07454ba4878..c99e747a2f9 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -6,6 +6,7 @@ from pygmt.helpers import ( build_arg_string, data_kind, + deprecate_parameter, fmt_docstring, is_nonstr_iter, kwargs_to_strings, @@ -43,6 +44,7 @@ t="transparency", ) @kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence") +@deprecate_parameter("sizes", "size", "v0.4.0", remove_version="v0.6.0") def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs): r""" Plot lines, polygons, and symbols in 2-D. From e12a50b33a96165e64d9a5e7998816d4ad29489c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 2 May 2021 21:58:43 -0400 Subject: [PATCH 4/6] Add a test for checking 'sizes' backward compatibility --- pygmt/tests/test_plot.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index d68c0f19249..952f54b718d 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -4,6 +4,7 @@ """ import datetime import os +import warnings import numpy as np import pandas as pd @@ -446,3 +447,28 @@ def test_plot_datetime(): y = [8.5, 9.5] fig.plot(x, y, style="i0.2c", pen="1p") return fig + + +@pytest.mark.mpl_image_compare(filename="test_plot_sizes.png") +def test_plot_deprecate_sizes_to_size(data, region): + """ + Make sure that the old parameter "sizes" is supported and it reports an + warning. + + Modified from the test_plot_sizes() test. + """ + fig = Figure() + with warnings.catch_warnings(record=True) as w: + fig.plot( + x=data[:, 0], + y=data[:, 1], + sizes=0.5 * data[:, 2], + region=region, + projection="X10c", + style="cc", + color="blue", + frame="af", + ) + assert len(w) == 1 + assert issubclass(w[0].category, FutureWarning) + return fig From d9a05cffa827fcf0f56f80a39f5ef53fabb47084 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 3 May 2021 23:10:04 -0400 Subject: [PATCH 5/6] Update sizes in docstrings --- examples/tutorials/plot.py | 2 +- pygmt/src/plot.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/tutorials/plot.py b/examples/tutorials/plot.py index cf3a0de1c42..cead5c55afb 100644 --- a/examples/tutorials/plot.py +++ b/examples/tutorials/plot.py @@ -63,7 +63,7 @@ ######################################################################################## # Notice that we didn't include the size in the ``style`` parameter this time, just the -# symbol ``c`` (circles) and the unit ``c`` (centimeter). So in this case, the sizes +# symbol ``c`` (circles) and the unit ``c`` (centimeter). So in this case, the size # will be interpreted as being in centimeters. # # We can also map the colors of the markers to the depths by passing an array to the diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index c99e747a2f9..60ff1540061 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -81,7 +81,7 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs): Use parameter ``columns`` to choose which columns are x, y, color, and size, respectively. size : 1d array - The sizes of the data points in units specified using ``style``. + The size of the data points in units specified using ``style``. Only valid if using ``x``/``y``. direction : list of two 1d arrays If plotting vectors (using ``style='V'`` or ``style='v'``), then @@ -220,7 +220,7 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs): if size is not None: if kind != "vectors": raise GMTInvalidInput( - "Can't use arrays for sizes if data is matrix or file." + "Can't use arrays for 'size' if data is a matrix or file." ) extra_arrays.append(size) From 64c763199dbc21b94cf195f80e42aa7b790a40f3 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 6 May 2021 17:48:38 -0400 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/tests/test_plot.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 952f54b718d..2fff587d292 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -4,7 +4,6 @@ """ import datetime import os -import warnings import numpy as np import pandas as pd @@ -458,7 +457,7 @@ def test_plot_deprecate_sizes_to_size(data, region): Modified from the test_plot_sizes() test. """ fig = Figure() - with warnings.catch_warnings(record=True) as w: + with pytest.warns(expected_warning=FutureWarning) as record: fig.plot( x=data[:, 0], y=data[:, 1], @@ -469,6 +468,5 @@ def test_plot_deprecate_sizes_to_size(data, region): color="blue", frame="af", ) - assert len(w) == 1 - assert issubclass(w[0].category, FutureWarning) + assert len(record) == 1 # check that only one warning was raised return fig