如何将列表中的文本数据标签添加到matplotlib图表中?

时间:2016-11-25 00:47:18

标签: python matplotlib

我已经建立了一个散点图,显示了Facebook喜欢的总数与参加体育赛事的人数。数据点标有“#of likes,total attendance”。

我想更改数据标签,以便显示每个事件的NAME。我将所有事件名称存储在名为Themes的变量中。此变量是通过从pandas数据框中的列中提取事件名称来创建的。这是我的代码:

from matplotlib import pyplot as plt

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

A = Likes
B = Attendance

plt.plot(A,B, "o")
for xy in zip(A, B):      
    ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data')

plt.grid()
plt.show()

如何更改此代码的标签部分以使用变量Themes中的每个事件的名称替换#of likes,total attendance?

1 个答案:

答案 0 :(得分:1)

试试这个:

plt.plot(Likes, Attendance, "o")
for (likes, attendance, theme) in zip(Likes, Attendance, Themes):      
    ax.annotate(theme, xy=(likes, attendance), textcoords='data')