使用pyplot显示数字而不是点 [英] Display numbers instead of points using pyplot
问题描述
我有一个nx2维数组,它表示n个点,它们的两个坐标分别是x,y.使用pyplot,我想显示我的点的数量n,而不是仅显示无法知道哪个是点的点.
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.
我该如何实现?
推荐答案
您可以使用plt.text
将数字作为文本放置在图中.要使数字出现在坐标的确切位置,可以使用ha="center", va="center"
居中对齐文本.
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)
这篇关于使用pyplot显示数字而不是点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!