learner
learner

Reputation: 157

Matplotlib Annotation Error

I tried finding solution to solve this error, but I can't get it.

# example data
month_no_list_svc_log = ["Nov", "Dec", "Jan", "Feb", "March", "April"]
event_count_by_month_list_svc_log = [10, 20, 30, 40, 50, 60]

fig, ax = plt.subplots(1,2, figsize = (10,4))
ax[0].plot(np.arange(len(month_no_list_svc_log)),event_count_by_month_list_svc_log)

# I do this to sort the months name the way I want it to
ax[0].set_xticks(np.arange(len(month_no_list_svc_log)))
ax[0].set_xticklabels(month_no_list_svc_log)

for i, txt in enumerate(event_count_by_month_list_svc_log):
    # the code below generates an error
    ax[0].annotate(txt, (month_no_list_svc_log[i],event_count_by_month_list_svc_log[i]))

I get an error:

'NoneType' object has no attribute 'seq'

Upvotes: 1

Views: 628

Answers (1)

Jeronimo
Jeronimo

Reputation: 2387

Your x-coordinate in annotate must be a value on the x-axis your provided to plot. There, you used the index of the months in month_no_list_svc_log, so this would match the value i in your for loop. Also you can reuse txt, here renamed to count, in the following way:

for i, count in enumerate(event_count_by_month_list_svc_log):
    ax[0].annotate(count, (i, count))

Btw: If you want your x-axis to be an arange of the same length as your y-axis data, you can just leave away the x-axis parameter in the call to plot:

ax[0].plot(event_count_by_month_list_svc_log)

Upvotes: 1

Related Questions