pythonmatplotlib

In matplotlib, why is the parameter for marker size not the same for different methods?


In matplotlib.pyplot.scatter, the parameter for marker size is s, while the parameter in other methods, such as plot, is simply markersize. I know that s is the marker size in points ** 2, so there is of course a difference compared to markersize, but why is it necessary to distinguish between s and markersize?

It seems, to me at least, that it would be simpler to just use one of the parameters for marker size in all types of plots.

I would like to know, as it can be difficult to remember, which parameter should be used in which plotting method.


Solution

  • Looking at the documentation for the different plot types, the reasoning is not explicitly stated, but we can infer that the reason for this difference would be that in a scatter plot, the s parameter is a positional argument with a default of None, and expects to receive an array of values as the size of the markers in a scatter plot is typically conveying additional information.

    Axes.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)

    While it will default to rcParams['lines.markersize'] ** 2 if no value is passed, the s parameter accepts either a float value to use as a constant for all points, or an array-like, shape (n,), giving a different size for each data point.

    s : float or array-like, shape (n, ), optional
    The marker size in points**2 (typographic points are 1/72 in.). Default is rcParams['lines.markersize'] ** 2.
    The linewidth and edgecolor can visually interact with the marker size, and can lead to artifacts if the marker size is smaller than the linewidth.
    If the linewidth is greater than 0 and the edgecolor is anything but 'none', then the effective size of the marker will be increased by half the linewidth because the stroke will be centered on the edge of the shape.
    To eliminate the marker edge either set linewidth=0 or edgecolor='none'.

    Because s is a positional parameter with a default, you don't have to pass it as a keyword argument; you can simply use scatter(x, y, s) where x, y, and s are all array-like objects with shape (n, ), or you can pass a float value for s.

    For the plot method, the markersize or ms argument is a keyword argument only, and only accepts a float. All of the other fully optional parameters are also passed as keyword arguments only. In fact, the plot method has no required positional arguments.

    Axes.plot(*args, scalex=True, scaley=True, data=None, **kwargs)