Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ac788d

Browse files
committed
Add markersize alias for s and markerscale kwarg
1 parent 10f0e4a commit 9ac788d

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4465,7 +4465,7 @@ def invalid_shape_exception(csize, xsize):
44654465
@_docstring.interpd
44664466
def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44674467
vmin=None, vmax=None, alpha=None, linewidths=None, *,
4468-
edgecolors=None, plotnonfinite=False, markersize=None, **kwargs):
4468+
edgecolors=None, plotnonfinite=False, markerscale=2, **kwargs):
44694469
"""
44704470
A scatter plot of *y* vs. *x* with varying marker size and/or color.
44714471
@@ -4545,10 +4545,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
45454545
or ``nan``). If ``True`` the points are drawn with the *bad*
45464546
colormap color (see `.Colormap.set_bad`).
45474547
4548-
markersize : float or array-like, shape (n, ), optional
4549-
The marker size in points. This differs from `s` as it sets the
4550-
size directly in points instead of points**2. `s` is used by
4551-
default to set the size if neither are passed.
4548+
markerscale : 1 or 2, optional, default: 2
4549+
Scaling factor used to set the size as points or points**2.
4550+
Default value is set as 2 to set the size values as points**2.
45524551
45534552
Returns
45544553
-------
@@ -4589,19 +4588,18 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
45894588
if x.size != y.size:
45904589
raise ValueError("x and y must be the same size")
45914590

4592-
if s is not None and markersize is not None:
4591+
if s is not None and 'markersize' in kwargs:
45934592
raise ValueError(
4594-
"Only one of s or markersize should be passed. "
4593+
"Only one of `s` or `markersize` should be passed. "
45954594
"Please refer to the docs for more details about usage.")
45964595

45974596
if s is None:
4598-
s = (20 if mpl.rcParams['_internal.classic_mode'] else
4599-
mpl.rcParams['lines.markersize'] ** 2.0)
4597+
if 'markersize' not in kwargs:
4598+
s = (20 if mpl.rcParams['_internal.classic_mode'] else
4599+
mpl.rcParams['lines.markersize'] ** 2.0)
4600+
else:
4601+
s = kwargs.pop('markersize')
46004602
s = np.ma.ravel(s)
4601-
markerscale = 2
4602-
if markersize is not None:
4603-
s = np.ma.ravel(markersize)
4604-
markerscale = 1
46054603
if (len(s) not in (1, x.size) or
46064604
(not np.issubdtype(s.dtype, np.floating) and
46074605
not np.issubdtype(s.dtype, np.integer))):

lib/matplotlib/collections.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -940,24 +940,43 @@ def update_from(self, other):
940940
self.stale = True
941941

942942

943+
@_api.define_aliases({
944+
"sizes": ["s", "markersize"]
945+
})
943946
class _CollectionWithSizes(Collection):
944947
"""
945948
Base class for collections that have an array of sizes.
946949
"""
947950
_factor = 1.0
948-
_markerscale = 2
949951

950952
def get_sizes(self):
951953
"""
952-
Return the sizes ('areas') of the elements in the collection.
954+
Return the sizes of the elements in the collection.
955+
956+
The sizes of the elements in the collection will be returned in
957+
the last set scale of the markers, i.e. if the markerscale was
958+
set to 1 then the sizes are set in points and if markerscale was
959+
set to 2 then the sizes returned are such that the marker size is
960+
in points**2.
953961
954962
Returns
955963
-------
956964
array
957-
The 'area' of each element.
965+
The 'size' of each element.
958966
"""
959967
return self._sizes
960968

969+
def get_markerscale(self):
970+
"""
971+
Return the scale used for marker sizing.
972+
973+
Returns
974+
-------
975+
int
976+
The scale used to set the marker sizes.
977+
"""
978+
return self._markerscale
979+
961980
def set_sizes(self, sizes, dpi=72.0, markerscale=2):
962981
"""
963982
Set the sizes of each member of the collection.
@@ -969,19 +988,19 @@ def set_sizes(self, sizes, dpi=72.0, markerscale=2):
969988
value is the 'area' of the element.
970989
dpi : float, default: 72
971990
The dpi of the canvas.
972-
markerscale : int, default: 2
973-
Scaling factor used to set the size as points or points**2.
974-
Default value is set as 2 used for `s` and changed to 1 if
975-
`markersize` is provided instead of `s`.
991+
markerscale : 1 or 2, default: 2
992+
Scaling factor used to set the size as points (1) or points**2 (2).
976993
"""
994+
# breakpoint()
995+
print(markerscale)
977996
self._markerscale = markerscale
978997
if sizes is None:
979998
self._sizes = np.array([])
980999
self._transforms = np.empty((0, 3, 3))
9811000
else:
9821001
self._sizes = np.asarray(sizes)
9831002
self._transforms = np.zeros((len(self._sizes), 3, 3))
984-
s = np.sqrt(self._sizes) if markerscale == 2 else self._sizes
1003+
s = np.sqrt(self._sizes) if self._markerscale == 2 else self._sizes
9851004
scale = s * dpi / 72.0 * self._factor
9861005
self._transforms[:, 0, 0] = scale
9871006
self._transforms[:, 1, 1] = scale
@@ -1009,6 +1028,8 @@ def __init__(self, paths, sizes=None, markerscale=2, **kwargs):
10091028
The factor by which to scale each drawn `~.path.Path`. One unit
10101029
squared in the Path's data space is scaled to be ``sizes**2``
10111030
points when rendered.
1031+
markerscale : 1 or 2, default: 2
1032+
Scaling factor used to set the size as points (1) or points**2 (2).
10121033
**kwargs
10131034
Forwarded to `.Collection`.
10141035
"""

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,13 +2831,13 @@ def quiverkey(Q, X, Y, U, label, **kwargs):
28312831
def scatter(
28322832
x, y, s=None, c=None, marker=None, cmap=None, norm=None,
28332833
vmin=None, vmax=None, alpha=None, linewidths=None, *,
2834-
edgecolors=None, plotnonfinite=False, markersize=None,
2834+
edgecolors=None, plotnonfinite=False, markerscale=2,
28352835
data=None, **kwargs):
28362836
__ret = gca().scatter(
28372837
x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm,
28382838
vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
28392839
edgecolors=edgecolors, plotnonfinite=plotnonfinite,
2840-
markersize=markersize,
2840+
markerscale=markerscale,
28412841
**({"data": data} if data is not None else {}), **kwargs)
28422842
sci(__ret)
28432843
return __ret

lib/matplotlib/tests/test_collections.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,3 +1216,25 @@ def test_striped_lines(fig_test, fig_ref, gapcolor):
12161216
for x, gcol, ls in zip(x, itertools.cycle(gapcolor),
12171217
itertools.cycle(linestyles)):
12181218
ax_ref.axvline(x, 0, 1, linestyle=ls, gapcolor=gcol, alpha=0.5)
1219+
1220+
1221+
@check_figures_equal(extensions=["png"])
1222+
def test_markerscale(fig_test, fig_ref):
1223+
ax_test = fig_test.add_subplot()
1224+
ax_ref = fig_ref.add_subplot()
1225+
1226+
x = np.arange(1, 6)**2
1227+
1228+
ax_test.scatter(x, x, s=x, markerscale=1)
1229+
ax_ref.scatter(x, x, s=x**2, markerscale=2)
1230+
1231+
1232+
@check_figures_equal(extensions=["png"])
1233+
def test_markersize_alias(fig_test, fig_ref):
1234+
ax_test = fig_test.add_subplot()
1235+
ax_ref = fig_ref.add_subplot()
1236+
1237+
x = np.arange(1, 6)**2
1238+
1239+
ax_test.scatter(x, x, s=x)
1240+
ax_ref.scatter(x, x, markersize=x)

0 commit comments

Comments
 (0)