Display numbers instead of points using pyplot [duplicate]

。_饼干妹妹 提交于 2019-12-01 12:39:27

问题


I have a nx2 dimension array representing n points with their two coordinates x, y. Using pyplot, I would like to display the number n of my points instead of just the points with no way to know which is what.

I have found a way to legend my points but really what I would like is only the number.

How can I achieve this ?


回答1:


You may use plt.text to place the number as text in the plot. To have the number appear at the exact position of the coordinates, you may center align the text using ha="center", va="center".

import numpy as np; np.random.seed(2)
import matplotlib.pyplot as plt

xy = np.random.rand(10,2)

plt.figure()
for i, ((x,y),) in enumerate(zip(xy)):
    plt.text(x,y,i, ha="center", va="center")

plt.show()

In order to have the plot autoscale to the range where the values are, you may add an invisible scatter plot

x,y =zip(*xy)
plt.scatter(x,y, alpha=0) 

or, if numbers are really small, better an invisible plot

x,y =zip(*xy)
plt.plot(x,y, alpha=0.0) 


来源:https://stackoverflow.com/questions/44630279/display-numbers-instead-of-points-using-pyplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!