Skip to content

Commit b51e3c2

Browse files
seismanweiji14
andauthored
Figure.plot: Deprecate parameter "sizes" to "size" (remove in v0.6.0) (#1254)
* Rename sizes to size in Figure.plot() * Updates tests and examples to use the new parameter name * Use the deprecate_parameter decorator for backward-compatibility * Add a test for checking 'sizes' backward compatibility Co-authored-by: Wei Ji <[email protected]>
1 parent ccf3046 commit b51e3c2

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

examples/gallery/symbols/points_categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
x=df.bill_length_mm,
5454
y=df.bill_depth_mm,
5555
# Vary each symbol size according to another feature (body mass, scaled by 7.5*10e-5)
56-
sizes=df.body_mass_g * 7.5e-5,
56+
size=df.body_mass_g * 7.5e-5,
5757
# Points colored by categorical number code
5858
color=df.species.cat.codes.astype(int),
5959
# Use colormap created by makecpt

examples/gallery/symbols/scatter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
)
2424
for color in ["gray73", "darkorange", "slateblue"]:
2525
x, y = np.random.rand(2, n) # random X and Y data in [0,1]
26-
sizes = np.random.rand(n) * 0.5 # random size [0,0.5], in cm
26+
size = np.random.rand(n) * 0.5 # random size [0,0.5], in cm
2727
# plot data points as circles (style="c"), with different sizes
2828
fig.plot(
2929
x=x,
3030
y=y,
3131
style="c",
32-
sizes=sizes,
32+
size=size,
3333
color=color,
3434
# Set the legend label,
3535
# and set the symbol size to be 0.25 cm (+S0.25c) in legend

examples/tutorials/plot.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
# parameter controls the outline of the symbols and the ``color`` parameter controls the fill.
4646
#
4747
# We can map the size of the circles to the earthquake magnitude by passing an array to
48-
# the ``sizes`` parameter. Because the magnitude is on a logarithmic scale, it helps to
48+
# the ``size`` parameter. Because the magnitude is on a logarithmic scale, it helps to
4949
# show the differences by scaling the values using a power law.
5050

5151
fig = pygmt.Figure()
@@ -54,7 +54,7 @@
5454
fig.plot(
5555
x=data.longitude,
5656
y=data.latitude,
57-
sizes=0.02 * (2 ** data.magnitude),
57+
size=0.02 * (2 ** data.magnitude),
5858
style="cc",
5959
color="white",
6060
pen="black",
@@ -63,7 +63,7 @@
6363

6464
########################################################################################
6565
# Notice that we didn't include the size in the ``style`` parameter this time, just the
66-
# symbol ``c`` (circles) and the unit ``c`` (centimeter). So in this case, the sizes
66+
# symbol ``c`` (circles) and the unit ``c`` (centimeter). So in this case, the size
6767
# will be interpreted as being in centimeters.
6868
#
6969
# We can also map the colors of the markers to the depths by passing an array to the
@@ -82,7 +82,7 @@
8282
fig.plot(
8383
x=data.longitude,
8484
y=data.latitude,
85-
sizes=0.02 * 2 ** data.magnitude,
85+
size=0.02 * 2 ** data.magnitude,
8686
color=data.depth_km,
8787
cmap=True,
8888
style="cc",

pygmt/src/plot.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pygmt.helpers import (
77
build_arg_string,
88
data_kind,
9+
deprecate_parameter,
910
fmt_docstring,
1011
is_nonstr_iter,
1112
kwargs_to_strings,
@@ -43,7 +44,8 @@
4344
t="transparency",
4445
)
4546
@kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence")
46-
def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
47+
@deprecate_parameter("sizes", "size", "v0.4.0", remove_version="v0.6.0")
48+
def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs):
4749
r"""
4850
Plot lines, polygons, and symbols in 2-D.
4951
@@ -78,8 +80,8 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
7880
Either a data file name or a 2d numpy array with the tabular data.
7981
Use parameter ``columns`` to choose which columns are x, y, color,
8082
and size, respectively.
81-
sizes : 1d array
82-
The sizes of the data points in units specified using ``style``.
83+
size : 1d array
84+
The size of the data points in units specified using ``style``.
8385
Only valid if using ``x``/``y``.
8486
direction : list of two 1d arrays
8587
If plotting vectors (using ``style='V'`` or ``style='v'``), then
@@ -215,12 +217,12 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
215217
)
216218
extra_arrays.append(kwargs["G"])
217219
del kwargs["G"]
218-
if sizes is not None:
220+
if size is not None:
219221
if kind != "vectors":
220222
raise GMTInvalidInput(
221-
"Can't use arrays for sizes if data is matrix or file."
223+
"Can't use arrays for 'size' if data is a matrix or file."
222224
)
223-
extra_arrays.append(sizes)
225+
extra_arrays.append(size)
224226

225227
for flag in ["I", "t"]:
226228
if flag in kwargs and is_nonstr_iter(kwargs[flag]):

pygmt/tests/test_plot.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ def test_plot_fail_no_data(data):
9393

9494
def test_plot_fail_color_size_intensity(data):
9595
"""
96-
Should raise an exception if array color, sizes and intensity are used with
96+
Should raise an exception if array color, size and intensity are used with
9797
matrix.
9898
"""
9999
fig = Figure()
100100
kwargs = dict(data=data, region=region, projection="X10c", frame="afg")
101101
with pytest.raises(GMTInvalidInput):
102102
fig.plot(style="c0.2c", color=data[:, 2], **kwargs)
103103
with pytest.raises(GMTInvalidInput):
104-
fig.plot(style="cc", sizes=data[:, 2], color="red", **kwargs)
104+
fig.plot(style="cc", size=data[:, 2], color="red", **kwargs)
105105
with pytest.raises(GMTInvalidInput):
106106
fig.plot(style="c0.2c", color="red", intensity=data[:, 2], **kwargs)
107107

@@ -152,7 +152,7 @@ def test_plot_sizes(data, region):
152152
fig.plot(
153153
x=data[:, 0],
154154
y=data[:, 1],
155-
sizes=0.5 * data[:, 2],
155+
size=0.5 * data[:, 2],
156156
region=region,
157157
projection="X10c",
158158
style="cc",
@@ -172,7 +172,7 @@ def test_plot_colors_sizes(data, region):
172172
x=data[:, 0],
173173
y=data[:, 1],
174174
color=data[:, 2],
175-
sizes=0.5 * data[:, 2],
175+
size=0.5 * data[:, 2],
176176
region=region,
177177
projection="X10c",
178178
style="cc",
@@ -193,7 +193,7 @@ def test_plot_colors_sizes_proj(data, region):
193193
x=data[:, 0],
194194
y=data[:, 1],
195195
color=data[:, 2],
196-
sizes=0.5 * data[:, 2],
196+
size=0.5 * data[:, 2],
197197
style="cc",
198198
cmap="copper",
199199
)
@@ -288,7 +288,7 @@ def test_plot_sizes_colors_transparencies():
288288
frame=True,
289289
style="cc",
290290
color=color,
291-
sizes=size,
291+
size=size,
292292
cmap="gray",
293293
transparency=transparency,
294294
)
@@ -446,3 +446,27 @@ def test_plot_datetime():
446446
y = [8.5, 9.5]
447447
fig.plot(x, y, style="i0.2c", pen="1p")
448448
return fig
449+
450+
451+
@pytest.mark.mpl_image_compare(filename="test_plot_sizes.png")
452+
def test_plot_deprecate_sizes_to_size(data, region):
453+
"""
454+
Make sure that the old parameter "sizes" is supported and it reports an
455+
warning.
456+
457+
Modified from the test_plot_sizes() test.
458+
"""
459+
fig = Figure()
460+
with pytest.warns(expected_warning=FutureWarning) as record:
461+
fig.plot(
462+
x=data[:, 0],
463+
y=data[:, 1],
464+
sizes=0.5 * data[:, 2],
465+
region=region,
466+
projection="X10c",
467+
style="cc",
468+
color="blue",
469+
frame="af",
470+
)
471+
assert len(record) == 1 # check that only one warning was raised
472+
return fig

0 commit comments

Comments
 (0)