2012-10-14 4 views
2

나는 3D 그래프를 가지고 있으며 좌표로 주석을 달고 싶습니다. 그러나 어노테이션은 겹쳐집니다. 나는 그들이 겹치지 않기를 바란다. 내 문제가주석으로 3D 점을 그려주는 Python

-

  • 주석이 전설에서
  • 을 중복 얻을, 나는 삼각형과 원의 두 심볼이있는 이유를 이해하지 않습니다. 그것은 단지 하나가 아니어야합니까?

내 정보는 다음 사항으로 제한됩니다. 따라서 다른 매개 변수가 하드 코딩 된 경우에도 괜찮습니다.

여기 내 코드입니다.

from mpl_toolkits.mplot3d import Axes3D 
from mpl_toolkits.mplot3d import proj3d 
import matplotlib.pyplot as plt 
import pylab  

xData1=[ 24500., 2980., 2980., 13740.] 
xData2=[ 8360., 8360., 24500., 5670., 2980., 2980., 11050., 13740.] 
yData1=[ 179., 244., 242., 181.] 
yData2=[ 132., 149., 116., 163., 247., 228., 116., 116.] 
zData1=[ 1., 44., 86., 44.] 
zData2=[ 86., 22., 1., 86., 43., 86., 86., 22.] 

fig = plt.figure() 
ax = fig.gca(projection='3d') 

ax.plot(xData1, yData1, zData1, '^', c='r', label='cfg1') 
ax.plot(xData2, yData2, zData2, 'o', c='b', label='cfg2') 


for i in range(len(xData1)): 
    text='['+str(int(xData1[i]))+','+str(int(yData1[i]))+','+str(int(zData1[i]))+']'  
    x2, y2, _ = proj3d.proj_transform(xData1[i],yData1[i],zData1[i], ax.get_proj())  
    label = pylab.annotate(text, 
    xycoords='data', 
    xy = (x2, y2), xytext = (60, 20), 
    textcoords = 'offset points', ha = 'right', va = 'bottom', 
    bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), 
    arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) 


for i in range(len(xData2)): 
    text='['+str(int(xData2[i]))+','+str(int(yData2[i]))+','+str(int(zData2[i]))+']'  
    x2, y2, _ = proj3d.proj_transform(xData2[i],yData2[i],zData2[i], ax.get_proj())  
    label = pylab.annotate(text, 
    xycoords='data', 
    xy = (x2, y2), xytext = (20, 20), 
    textcoords = 'offset points', ha = 'right', va = 'bottom', 
    bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), 
    arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) 

ax.set_xlabel('X-Data') 
ax.set_ylabel('Y-Data') 
ax.set_zlabel('Z-Data') 

ax.legend(ncol=3) 
plt.show() 

답변

1

두 질문 모두 비교적 쉬운 대답입니다. 두 번째부터 시작하겠습니다. 범례를 정의 할 때 숫자를 지정하지 않았고 기본값이 2이므로 숫자에는 두 개의 기호가 있습니다. 수정하려면 변경 :

numpoints 범례 내에서 점의 수를 변경
ax.legend(ncol=3, numpoints=1) 

- 지금은 텍스트 주석의 위치를 ​​조작하는 것을 포함하여 첫 번째 질문 1.

대답에 설정되어, 더 많은 특히 xytext은 텍스트의 좌표를 제공합니다. 아래에 대한 루프 두 번째 교체하면 중복 텍스트 없애 당신에게 다른보기 흉한 위치 문제에 대한 주석 상자의 위치를 ​​변경하는 방법의 좋은 예를 제공한다 :

for i in range(len(xData2)): 
    text='['+str(int(xData2[i]))+','+str(int(yData2[i]))+','+str(int(zData2[i]))+']' 
    x2, y2, _ = proj3d.proj_transform(xData2[i],yData2[i],zData2[i], ax.get_proj()) 
    if i==4: 
     label = pylab.annotate(text, 
         xycoords='data', 
         xy = (x2, y2), xytext = (0, -50), 
         textcoords = 'offset points', ha = 'right', va = 'bottom', 
         bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), 
         arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) 
    elif i==6: 
     label = pylab.annotate(text, 
          xycoords='data', 
          xy = (x2, y2), xytext = (-40, 0), 
          textcoords = 'offset points', ha = 'right', va = 'bottom', 
          bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), 
          arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) 
    else: 
     label = pylab.annotate(text, 
          xycoords='data', 
          xy = (x2, y2), xytext = (-20, 10), 
          textcoords = 'offset points', ha = 'right', va = 'bottom', 
          bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), 
          arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) 
+0

이 위대하다! :) – Raj

관련 문제