2017-04-12 70 views
1

我已經寫了一個腳本,在建築物的地面圖上繪製標記(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

回答

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

相關問題