可以使用經縮放的變換在相同的位置作爲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()