我从你的另一个stackoverflow question 跳进来。我认为您作为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):