2016-07-03 3 views
1

나는 ... 산포도 주석이 코드 같은 것을 적용하려면 플롯 생산포인트를 가로로 주석을 중앙에 배치하는 방법은 무엇입니까?

import matplotlib; matplotlib.use('TkAgg') 
import matplotlib.pyplot as plt 

labels = ["Abra", "Kadabra", "Alazkazam", "Mew"] 
x_values = [0.3, 0.6, 0.2, 0.4] 
y_values = [0.2, 0.2, 0.4, 0.9] 

fig = plt.figure(figsize=(5, 5)) 
plt.axis('off') 

renderer = fig.canvas.get_renderer() 

for i, label in enumerate(labels): 
    plt.scatter(x_values[i], y_values[i]) 
    text_object = plt.annotate(label, xy=(x_values[i], y_values[i])) 

plt.savefig("horizontally_centered_text_annotations.png") 

... 싶습니다 annotations without centering

을 ... 그리고 이런 식으로 뭔가를 생산하게 줄거리 : 나는 텍스트 상자 주위에 윈도우 범위를 점점 시도했습니다 annotations with centering

, x 좌표와 폭,과 같이 각 주석에 대한 이상 이동 잡는 :

0 당신이 인쇄 문에서 여기 볼 수
for i, label in enumerate(labels): 
    plt.scatter(x_values[i], y_values[i]) 
    text_object = plt.annotate(label, xy=(x_values[i], y_values[i])) 
    text_window_extent = text_object.get_window_extent(renderer) 
    new_x_position = x_values[i] - text_window_extent.width/2 
    text_object.set_position((new_x_position, y_values[i])) 
    print "x_value: {}, window_extent_width: {}, new_x_position: {}".format(x_values[i], text_window_extent.width, new_x_position) 

는하지만 폭이 너무 큰 :

x_value: 0.3, window_extent_width: 31.5, new_x_position: -15.45 
x_value: 0.6, window_extent_width: 56.0, new_x_position: -27.4 
x_value: 0.2, window_extent_width: 72.875, new_x_position: -36.2375 
x_value: 0.4, window_extent_width: 30.75, new_x_position: -14.975 

하지 않음이있는 경우 좌표계를 함께 할 방법을 잘 ...

답변

3

사용 horizontalalignment (이

text_object = plt.annotate(label, xy=(x_values[i], y_values[i]), ha='center') 
+0

가 완벽하게 작동 다음 annotate 호출 ha) 옵션으로 단축 할 수있다. – legel

관련 문제