2016-07-30 68 views
1

我想用plot(x,y)绘制的东西,能够看到在图上值的指数x在情节

例如在x刻度显示两者的价值和指数。

x = [10,30,100,120] 
y = [16,17,19,3] 
plot(x,y) 

将显示

enter image description here

看剧情是很难知道在每一点上什么是原来的指数。例如,我希望能够知道从何时开始(100,19)它是索引2,因为x[2]=100y[2]=19。 我该如何在matplotlib中做到这一点。 我看着twiny()函数,但似乎只是添加另一个轴,忽略x中点之间的距离。

回答

1

这里是我的解决方案:

import matplotlib.pyplot as plt 
x = [10,30,100,120] 
y = [16,17,19,3] 
plt.plot(x,y); 
for i, (a, b) in enumerate(zip(x, y)): 
    plt.annotate(str(i), xy=(a, b), textcoords="offset points", xytext=(0, 12), 
       horizontalalignment='center', verticalalignment='center') 
plt.xlim(0, 130) 
plt.ylim(0, 22) 

做些什么:它列举了您的yx阵列,存储在变量i指数和中变量xyab各自的值。然后在坐标(a, b)处注释索引i,在y轴上将文本偏移12个像素以避免覆盖曲线的注释。

结果:

enter image description here

+0

对于非常大的图形,即1000点的可能缓慢运行,否则,这是好的。谢谢! – tal