각 데이터 포인트에서 다른 텍스트를 가진 Matplotlib 산점도 n. y = [2.56422, 3.77284, 3.52623, 3.51468,

산점도를 만들고 목록에서 다른 숫자로 데이터 포인트에 주석을 달려고합니다. 예를 들어, yvs x에 대응하는 숫자 로 플롯 하고 주석을 달고 싶습니다 n.

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]
ax = fig.add_subplot(111)
ax1.scatter(z, y, fmt='o')

어떤 아이디어?



답변

배열이나 목록을 취하는 플로팅 방법을 모르지만 annotate()의 값을 반복 하는 동안 사용할 수 있습니다 n.

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()
ax.scatter(z, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i], y[i]))

에 대한 많은 서식 옵션이 annotate()있습니다. matplotlib 웹 사이트를 참조하십시오 .

여기에 이미지 설명을 입력하십시오


답변

matplotlib 2.0 이전 버전에서는 ax.scatter마커없이 텍스트를 플롯 할 필요가 없습니다. 버전 2.0에서는 ax.scatter텍스트의 적절한 범위와 마커를 설정 해야 합니다.

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i], y[i]))

링크 에서 3D로 예제를 찾을 수 있습니다.


답변

누구나 위의 솔루션을 .subplot () 대신 .scatter ()에 적용하려고하는 경우,

다음 코드를 실행하려고했습니다.

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.scatter(z, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i], y[i]))

그러나 “반복 불가능한 PathCollection 객체의 압축을 풀 수 없습니다”라는 오류가 발생했습니다. 오류는 특히 코드 라인 fig, ax = plt.scatter (z, y)를 가리 킵니다.

결국 다음 코드를 사용하여 오류를 해결했습니다.

plt.scatter(z, y)

for i, txt in enumerate(n):
    plt.annotate(txt, (z[i], y[i]))

.scatter ()와 .subplot () 사이에 더 잘 알고 있어야 할 차이점이 있다고 생각하지 않았습니다.


답변

사용할 수도 있습니다 pyplot.text( 여기 참조 ).

def plot_embeddings(M_reduced, word2Ind, words):
""" Plot in a scatterplot the embeddings of the words specified in the list "words".
    Include a label next to each point.
"""
for word in words:
    x, y = M_reduced[word2Ind[word]]
    plt.scatter(x, y, marker='x', color='red')
    plt.text(x+.03, y+.03, word, fontsize=9)
plt.show()

M_reduced_plot_test = np.array([[1, 1], [-1, -1], [1, -1], [-1, 1], [0, 0]])
word2Ind_plot_test = {'test1': 0, 'test2': 1, 'test3': 2, 'test4': 3, 'test5': 4}
words = ['test1', 'test2', 'test3', 'test4', 'test5']
plot_embeddings(M_reduced_plot_test, word2Ind_plot_test, words)

여기에 이미지 설명을 입력하십시오


답변

파이썬 3.6 이상 :

coordinates = [('a',1,2), ('b',3,4), ('c',5,6)]
for x in coordinates: plt.annotate(x[0], (x[1], x[2]))


답변

목록 이해와 numpy를 사용하는 하나의 라이너로 :

[ax.annotate(x[0], (x[1], x[2])) for x in np.array([n,z,y]).T]

설정은 Rutger의 답변과 다릅니다.


답변

화살표 / 텍스트 상자를 사용하여 레이블에 주석을 달 수도 있다고 덧붙이고 싶습니다. 여기 내가 의미하는 바가있다 :

import random
import matplotlib.pyplot as plt


y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()
ax.scatter(z, y)

ax.annotate(n[0], (z[0], y[0]), xytext=(z[0]+0.05, y[0]+0.3), 
    arrowprops=dict(facecolor='red', shrink=0.05))

ax.annotate(n[1], (z[1], y[1]), xytext=(z[1]-0.05, y[1]-0.3), 
    arrowprops = dict(  arrowstyle="->",
                        connectionstyle="angle3,angleA=0,angleB=-90"))

ax.annotate(n[2], (z[2], y[2]), xytext=(z[2]-0.05, y[2]-0.3), 
    arrowprops = dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1))

ax.annotate(n[3], (z[3], y[3]), xytext=(z[3]+0.05, y[3]-0.2), 
    arrowprops = dict(arrowstyle="fancy"))

ax.annotate(n[4], (z[4], y[4]), xytext=(z[4]-0.1, y[4]-0.2),
    bbox=dict(boxstyle="round", alpha=0.1), 
    arrowprops = dict(arrowstyle="simple"))

plt.show()

다음 그래프가 생성됩니다.
여기에 이미지 설명을 입력하십시오