python 散点图面积大小比例轴长度

python scatter plot area size proportional axis length

我对此感到非常绝望,到目前为止我在 www 上找不到任何东西。

情况如下:

到目前为止,一切都按照我想要的方式进行。这是困扰我的事情:

我正在苦苦挣扎的是找出绘图点与轴长度的比率。我需要处理点,因为据我所知,散点图的圆大小以点值给出。所以如果假设我的轴从 0 到 10,我需要知道图中有多少个点。

有人可以帮助我吗?或者还有另一种方法吗? 提前致谢。

我从你的另一个跳入 Whosebug question. I think the approach you presented as an answer to the present question 由于以下原因不能完全按照你想要的方式工作:

  • 首先,标记的大小是以点为单位,而不是以像素为单位。在排版中,the point 是最小的度量单位,在 matplotlib 中对应于 1/72 英寸的固定长度。相比之下,一个像素的大小会随着图形dpi和大小的变化而变化。
  • 其次,plt.scatter中标记的大小与圆的直径有关,与半径无关。

因此每个标记点的大小应计算为:

size_in_points = (2 * radius_in_pixels / fig_dpi * 72 points/inch)**2

此外,如下面的 MWE 所示,可以直接使用 matplotlib transformations 计算标记半径的大小(以像素为单位),而无需事先生成空图形:

import numpy as np
import matplotlib.pyplot as plt

plt.close('all')

# Generate some data :
N = 25
x = np.random.rand(N) + 0.5
y = np.random.rand(N) + 0.5
r = np.random.rand(N)/10

# Plot the data :
fig = plt.figure(facecolor='white', figsize=(7, 7))
ax = fig.add_subplot(111, aspect='equal')
ax.grid(True)
scat = ax.scatter(x, y, s=0, alpha=0.5, clip_on=False)
ax.axis([0, 2, 0, 2])

# Draw figure :
fig.canvas.draw()

# Calculate radius in pixels :
rr_pix = (ax.transData.transform(np.vstack([r, r]).T) -
          ax.transData.transform(np.vstack([np.zeros(N), np.zeros(N)]).T))
rpix, _ = rr_pix.T

# Calculate and update size in points:
size_pt = (2*rpix/fig.dpi*72)**2
scat.set_sizes(size_pt)

# Save and show figure:
fig.savefig('scatter_size_axes.png')
plt.show()

在 (1, 1) 处指定半径为 0.5 的点将在绘图中生成一个以 (1, 1) 为中心且边界穿过点 (1.5, 1), (1, 1.5) 的圆), (0.5, 1) 和 (1, 0.5):