python scatter点的大小_pyplot散点图标记大小 (pyplot scatter plot marker size)

在Python的matplotlib库中,`plt.scatter`的`s`参数表示散点的面积(以点的平方为单位)。s值是实际标记大小的平方,与线图的markersize不同,需要平方后传递。对于正方形标记,面积直接等于`s`值;圆形标记的面积是`s*pi/4`。虽然`s`参数通常称为“面积”,但在非正方形标记中可能与实际面积无关,但与标记感知的面积成比例。点的大小以每英寸72点的标准定义,可以按像素指定大小以保持一致的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Because other answers here claim that s denotes the area of the marker, I'm adding this answer to clearify that this is not necessarily the case.

Size in points^2

The argument s in plt.scatter denotes the markersize**2. As the documentation says

s : scalar or array_like, shape (n, ), optional

size in points^2. Default is rcParams['lines.markersize'] ** 2.

This can be taken literally. In order to obtain a marker which is x points large, you need to square that number and give it to the s argument.

So the relationship between the markersize of a line plot and the scatter size argument is the square. In order to produce a scatter marker of the same size as a plot marker of size 10 points you would hence call scatter( .., s=100).

import matplotlib.pyplot as plt

fig,ax = plt.subplots()

ax.plot([0],[0], marker="o", markersize=10)

ax.plot([0.07,0.93],[0,0], linewidth=10)

ax.scatter([1],[0], s=100)

ax.plot([0],[1], marker="o", markersize=22)

ax.plot([0.14,0.86],[1,1], linewidth=22)

ax.scatter([1],[1], s=22**2)

plt.show()

Connection to "area"

So why do other answers and even the documentation speak about "area" when it comes to the s parameter?

Of course the units of points**2 are area units.

For the special case of a square marker, marker="s", the area of the marker is indeed directly the value of the s parameter.

For a circle, the area of the circle is area = pi/4*s.

For other markers there may not even be any obvious relation to the area of the marker.

In all cases however the area of the marker is proportional to the s parameter. This is the motivation to call it "area" even though in most cases it isn't really.

Specifying the size of the scatter markers in terms of some quantity which is proportional to the area of the marker makes in thus far sense as it is the area of the marker that is perceived when comparing different patches rather than its side length or diameter. I.e. doubling the underlying quantity should double the area of the marker.

What are points?

So far the answer to what the size of a scatter marker means is given in units of points. Points are often used in typography, where fonts are specified in points. Also linewidths is often specified in points. The standard size of points in matplotlib is 72 points per inch (ppi) - 1 point is hence 1/72 inches.

It might be useful to be able to specify sizes in pixels instead of points. If the figure dpi is 72 as well, one point is one pixel. If the figure dpi is different (matplotlib default is fig.dpi=100),

1 point == fig.dpi/72. pixels

While the scatter marker's size in points would hence look different for different figure dpi, one could produce a 10 by 10 pixels^2 marker, which would always have the same number of pixels covered:

import matplotlib.pyplot as plt

for dpi in [72,100,144]:

fig,ax = plt.subplots(figsize=(1.5,2), dpi=dpi)

ax.set_title("fig.dpi={}".format(dpi))

ax.set_ylim(-3,3)

ax.set_xlim(-2,2)

ax.scatter([0],[1], s=10**2,

marker="s", linewidth=0, label="100 points^2")

ax.scatter([1],[1], s=(10*72./fig.dpi)**2,

marker="s", linewidth=0, label="100 pixels^2")

ax.legend(loc=8,framealpha=1, fontsize=8)

fig.savefig("fig{}.png".format(dpi), bbox_inches="tight")

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值