如何让一个注释指向matplotlib中的多个点?

2024-05-23 20:11:59 发布

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

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


Tags: 数据方法标记matplotlib绘制箭头指向气球
3条回答

怎么样(基本上是从文档中取出的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

这种形式对你更好吗?在

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

你在找类似的吗?在

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

相关问题 更多 >