控制两个标记之间相对于标记的空间

时间:2017-04-12 09:24:44

标签: matplotlib

我写了一个脚本,在建筑物的地面图上绘制标记(X)。另外,我想相对于每个标记添加彩色点,到目前为止工作正常(如下图所示)。我使用以下代码绘制点,如您所见,我使用单个标记的坐标,然后将点相对于X标记的位置放置。

plt.plot((((coordinates[i][1]-0.5*(j+1.8)))),(((coordinates[i][2]+0.5*(k-1)))),...)

然而,我想要做的是将点相对于X标记放置而不是真正的坐标,而是将标记尺寸放置x倍,如下所示:

plt.plot((((coordinates[i][1]-0.5*(j+markersize)))),(((coordinates[i][2]+0.5*(k-markersize)))),...)

有一种简单的方法吗?

非常感谢你的帮助!

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用缩放变换在与X标记相同的位置绘制点,但使用以markersize为单位指定的偏移。

import matplotlib.pyplot as plt
import matplotlib.transforms

fig, ax = plt.subplots()

def offsetpoint(x,y, px,py, msize, **kwargs):
    dx = px * msize/72.; dy = py * msize/72.
    to = matplotlib.transforms.ScaledTranslation(dx,dy,fig.dpi_scale_trans)
    trans = ax.transData + to
    ax.plot(x,y, transform=trans, marker="o", ms=msize/4.7,**kwargs)

ms = 7
ax.plot([2],[3], ms=ms, marker="x",color="crimson")
offsetpoint([2],[3], -1,-0.5, ms, color="crimson")
offsetpoint([2],[3], -1,0 ,   ms, color="limegreen")
offsetpoint([2],[3], -1,0.5,  ms, color="gold")
offsetpoint([2],[3], -1.5,-0.5, ms, color="gold")
offsetpoint([2],[3], -1.5,0,    ms, color="limegreen")
offsetpoint([2],[3], -1.5,0.5,  ms, color="crimson")

ms = 15
ax.plot([1.5],[1.5], ms=ms, marker="x",color="mediumblue")
offsetpoint([1.5],[1.5], -1,-0.5, ms, color="darkviolet")
offsetpoint([1.5],[1.5], -1,0 ,   ms, color="turquoise")
offsetpoint([1.5],[1.5], -1,0.5,  ms, color="gold")
offsetpoint([1.5],[1.5], -1.5,-0.5, ms, color="darkviolet")
offsetpoint([1.5],[1.5], -1.5,0,    ms, color="limegreen")
offsetpoint([1.5],[1.5], -1.5,0.5,  ms, color="indigo")

ax.margins(x=0.5, y=0.5)
plt.show()

enter image description here