python - 如何调整散点图的标记大小,使其与给定的半径相匹配? (使用 matplotlib 转换)

标签 python matplotlib transform marker

我想让散点图的标记与数据坐标中给定的半径相匹配。

我读过 pyplot scatter plot marker size ,标记大小以标记的面积形式给出,以点 ^2 为单位。

我尝试通过 axes.transData 将给定的半径转换为点,然后通过 pi * r^2 计算面积,但我没有成功。

也许我做错了转换。 也可能是通过 VcXsrv 从 WSL 运行 matplotlib 导致了这个问题。

这是我想要完成的示例代码,结果作为代码下方的图像:

import matplotlib.pyplot as plt
from numpy import pi

n = 16
# create a n x n square with a marker at each point
x_data = []
y_data = []
for x in range(n):
    for y in range(n):
        x_data.append(x)
        y_data.append(y)

fig,ax = plt.subplots(figsize=[7,7])

# important part:
# calculate the marker size so that the markers touch

# radius in data coordinates:
r = 0.5 
# radius in display coordinates:
r_ = ax.transData.transform([r,0])[0] - ax.transData.transform([0,0])[0]
# marker size as the area of a circle
marker_size = pi * r_**2

ax.scatter(x_data, y_data, s=marker_size, edgecolors='black')

plt.show()

当我使用 s=r_ 运行它时,我得到的结果在左侧,而使用 s=marker_size 我得到的结果在下图的右侧: Example

最佳答案

代码看起来非常好。如果您仅绘制 4 个点 (n=2),您就会看到这一点:

4 cricles

半径(几乎)正好是您想要的 r=0.5 坐标单位。等等,几乎?! 是的,问题是您在绘图之前确定了坐标单位到图形点的大小,因此设置限制之前,这会影响坐标单位而不是整体图形大小...

听起来很奇怪?也许。最重要的是,您使用默认轴限制 ((0,1) x (0,1)) 确定坐标变换,然后将它们放大到 (-0.75, 15.75)x(-0.75, 15.75)...但您并未减小标记大小。

因此要么在绘图之前将限制设置为已知大小:

ax.set_xlim((0,n-1))
ax.set_ylim((0,n-1))

完整代码为:

import matplotlib.pyplot as plt
from numpy import pi

n = 16
# create a n x n square with a marker at each point as dummy data
x_data = []
y_data = []
for x in range(n):
    for y in range(n):
        x_data.append(x)
        y_data.append(y)

# open figure
fig,ax = plt.subplots(figsize=[7,7])
# set limits BEFORE plotting
ax.set_xlim((0,n-1))
ax.set_ylim((0,n-1))
# radius in data coordinates:
r = 0.5 # units
# radius in display coordinates:
r_ = ax.transData.transform([r,0])[0] - ax.transData.transform([0,0])[0] # points
# marker size as the area of a circle
marker_size = pi * r_**2
# plot
ax.scatter(x_data, y_data, s=marker_size, edgecolors='black')

plt.show()

output set limits

...或根据新限制缩放标记的大小(您将需要了解它们或重新绘图)

# plot with invisible color
ax.scatter(x_data, y_data, s=marker_size, color=(0,0,0,0))
# calculate scaling
scl = ax.get_xlim()[1] - ax.get_xlim()[0]
# plot correctly (with color)
ax.scatter(x_data, y_data, s=marker_size/scl**2, edgecolors='blue',color='red')

这是一个相当乏味的想法,因为您需要绘制两次数据,但您保持轴的自动调整大小...

output with double plotting

显然还有一些间距。这是由于对标记的区域的误解。我们不是在谈论符号的面积(在这种情况下是一个圆圈),而是标记的边界框(想象一下,你想控制星星或星号的大小作为标记......人们永远不会计算符号的实际面积)。 所以计算面积不是 pi * r_**2 而是一个正方形:(2*r_)**2

# open figure
fig,ax = plt.subplots(figsize=[7,7])
# setting the limits
ax.set_xlim((0,n-1))
ax.set_ylim((0,n-1))
# radius in data coordinates:
r = 0.5 # units
# radius in display coordinates:
r_ = ax.transData.transform([r,0])[0] - ax.transData.transform([0,0])[0] # points
# marker size as the area of a circle
marker_size = (2*r_)**2
# plot
ax.scatter(x_data, y_data, s=marker_size,linewidths=1)
#ax.plot(x_data, y_data, "o",markersize=2*r_)
plt.show()

output (2*r)**2 只要您添加一条边(标记周围的非零边界),它们就会重叠: overlap

如果您使用 plot(如果所有标记都应具有与 docs 状态相同的大小,则速度会更快,如“注释”)。 markersize 只是标记的宽度(不是面积):

ax.plot(x_data, y_data, "o",markersize=2*r_,color='magenta')

plot command

关于python - 如何调整散点图的标记大小,使其与给定的半径相匹配? (使用 matplotlib 转换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65174418/

相关文章:

python - 将 python 列表插入 SQLite 数据库

css - Safari(手机+桌面)分组CSS关键帧动画

xml - 在 Visual Basic 9 中将一个元素转换为另一个元素的 LINQ 或 XSLT

python - 如何计算Python pandas中最后五行的平均值

python - TypeError: a bytes-like object is required, not 'str' – 在Python中保存JSON数据

python - python中相同生成器的链式重复

python - 使用 Matplotlib 自定义基于时间序列的数据的 x Axis

python - 使用 matplotlib 创建相关图

python - 使用 cartopy 在其他投影中绘制投影数据

c++ - 转换和积累