假设我想自动选择散点图中方形符号的大小以使边界对齐(已经提出了类似的问题)。
在我对这个问题的回答中,我建议可以使用以像素为单位测量的两个数据点之间的距离来设置散点图中符号的大小。
这是我的方法(它的灵感来自这个答案):
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
# initialize a plot to determine the distance between the data points in pixel:
x = [1, 2, 3, 4, 2, 3, 3]
y = [0, 0, 0, 0, 1, 1, 2]
s = 0.0
points = ax.scatter(x,y,s=s,marker='s')
ax.axis([min(x)-1., max(x)+1., min(y)-1., max(y)+1.])
# retrieve the pixel information:
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
xpix, ypix = xy_pixels.T
# In matplotlib, 0,0 is the lower left corner, whereas it's usually the upper
# right for most image software, so we'll flip the y-coords
width, height = fig.canvas.get_width_height()
ypix = height - ypix
# this assumes that your data-points are equally spaced
s1 = xpix[1]-xpix[0]
# the marker size is given as points^2, hence s1**2.
points = ax.scatter(x,y,s=s1**2.,marker='s',edgecolors='none')
ax.axis([min(x)-1., max(x)+1., min(y)-1., max(y)+1.])
fig.savefig('test.png', dpi=fig.dpi)
但是,使用这种方法,符号会重叠。我可以手动调整符号大小,使它们对齐但不重叠:
s1 = xpix[1]-xpix[0] - 13.
13
可以事先确定调整(在这种情况下为)吗?- 一般方法的缺陷是什么,需要调整?