Andrew Smith Andrew Smith - 1 year ago 161
Python Question

How do I add text data labels from a list to matplotlib chart?

I have built a scatter plot showing the Total Number of Facebook Likes vs. the Attendance at a list of Sporting Events. The datapoints are labeled with "#of likes, total attendance."

I'd like to alter the data labels so that they show instead the NAME of each event. I have all of the event names stored in a variable called

. This variable was created by pulling the event names from a column in a pandas dataframe. Here is my code:

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')


How can I alter the label portion of this code to replace the #of likes, total attendance with the names of each event from my variable

Answer Source

Try this:

plt.plot(Likes, Attendance, "o")
for (likes, attendance, theme) in zip(Likes, Attendance, Themes):      
    ax.annotate(theme, xy=(likes, attendance), textcoords='data')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download