如何在matplotlib中有一个注释指向几个点?

时间:2013-07-01 21:27:57

标签: python matplotlib

我有一些我通常在matplotlib中绘制的数据。自变量的某些值是共振,我想用类似于matplotlib annotate的东西来标记它们。有没有办法让一个注释(一个气球说“共振”之类的东西)用箭头指向情节上的几个点?

3 个答案:

答案 0 :(得分:3)

这种形式对你有好处吗?

import matplotlib.pyplot as plt
import numpy as np
a = np.ones(100)
multi = np.arange(0,100,5)
plt.ylim(-0.5,10)
plt.text(50, 6.5,'a=5k',fontsize=20)
for x in multi:
    plt.annotate("",xy=(x,1),xytext=(50,6),
                 arrowprops=dict(facecolor='black', shrink=0.005))
plt.plot(a,'k.')
plt.show()

enter image description here

答案 1 :(得分:1)

您正在寻找类似的?

import matplotlib.pyplot as plt
import numpy as np
a = np.ones(100)
multi = np.arange(0,100,5)
plt.ylim(-0.5,10)
for x in multi:
    plt.annotate("a=5k",xy=(x,1),xytext=(x,1+4*np.random.rand()),
                 arrowprops=dict(facecolor='black', shrink=0.05))
plt.plot(a,'k.')
plt.show()

enter image description here

答案 2 :(得分:1)

怎么样(基本上脱离了文档http://matplotlib.org/users/annotations_intro.html

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=2)

coords_to_annote = [(2,1),(3,1),(4,1)]

for coords in coords_to_annote:
    ax.annotate('local max', xy=coords, xytext=(3, 1.5),
                arrowprops=dict(facecolor='black', shrink=0.05),
                )


ax.set_ylim(-2,2)
plt.show()

enter image description here