使用数字作为matplotlib打印标记

2024-05-04 05:58:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个一维numpy数组,我想绘制这个数组,我希望plot标记是一个数字,显示元素的位置。例如,如果我的数组是[2.5,4,3],那么我希望绘图在点(0,2.5)处有0,在(1,4)处有1,在(2,3)处有2,依此类推。

怎么做?


Tags: 标记numpy元素绘图plot绘制数字数组
2条回答

使用pylab可能会被劝阻(我必须查一下pylab是什么)。

import matplotlib.pyplot as plt
xs = [0, 1, 2]
ys = [2.5, 4, 3]
plt.plot(xs, ys, "-o")
for x, y in zip(xs, ys):
    plt.text(x, y, str(x), color="red", fontsize=12)

您需要在for循环中调用pylab.text()

import pylab as pl
xs = [0, 1, 2]
ys = [2.5, 4, 3]
pl.plot(xs, ys, "-o")
for x, y in zip(xs, ys):
    pl.text(x, y, str(x), color="red", fontsize=12)
pl.margins(0.1)

enter image description here

相关问题 更多 >