假设我想在散点图中自动选择方形符号的大小,以使边框对齐(a question like that has been asked)。
在这个问题的my answer中,我建议用像素测量的两个数据点之间的距离可以用来设置散点图中符号的大小。
这是我的方法(它的灵感来自this answer):
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
)吗?