I am trying to make a scatter plot and annotate data points with different numbers from a list.
So, for example, I want to plot y vs x and annotate with corresponding numbers from 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')
Any ideas?
回答 0
我不知道有任何采用数组或列表的绘图方法,但可以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]))
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]))
In version’s earlier than matplotlib 2.0, ax.scatter is not necessary to plot text without markers. In version 2.0 you’ll need ax.scatter to set the proper range and markers for text.
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]))
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]))
In case anyone is trying to apply the above solutions to a .scatter() instead of a .subplot(),
I tried running the following code
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]))
But ran into errors stating “cannot unpack non-iterable PathCollection object”, with the error specifically pointing at codeline fig, ax = plt.scatter(z, y)
I eventually solved the error using the following code
plt.scatter(z, y)
for i, txt in enumerate(n):
plt.annotate(txt, (z[i], y[i]))
I didn’t expect there to be a difference between .scatter() and .subplot()
I should have known better.
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)
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)
回答 4
Python 3.6及更高版本:
coordinates =[('a',1,2),('b',3,4),('c',5,6)]for x in coordinates: plt.annotate(x[0],(x[1], x[2]))