gpt4 book ai didi

python - 以 numpy 行索引为标记的 Matplotlib 散点图

转载 作者:太空狗 更新时间:2023-10-30 00:48:02 25 4
gpt4 key购买 nike

我有一个 numpy 数组,我正在尝试使用 matplotlib 用散点图绘制它。

from matplotlib import pyplot as plt
from matplotlib import pylab as pl

pl.plot(matrix[:,0],matrix[:,1], 'ro')

这给了我类似的东西: plot

现在我想用与 numpy 数组中行的索引相对应的数字替换红点。我该怎么做?

谢谢!

最佳答案

您可以使用 plt.text 执行此操作:

from matplotlib import pyplot as plt

import numpy as np
N = 100
matrix = np.random.rand(N,2)

plt.plot(matrix[:,0],matrix[:,1], 'ro', alpha = 0.5)
for i in range(matrix.shape[0]):
plt.text(matrix[i,0], matrix[i,1], str(i))

plt.show()

enter image description here

如果要替换红点,则设置alpha = 0.0:

enter image description here

关于python - 以 numpy 行索引为标记的 Matplotlib 散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com