Mark K
Mark K

Reputation: 9348

Scatter plot to add Dates as data labels

Plotting scatters I am using below:

import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

dates = ['2015-12-20','2015-09-12','2015-08-12','2015-06-12']  
PM_25 = [68, 66, 55, 46]

dates = [pd.to_datetime(d) for d in dates]

plt.scatter(dates, PM_25, s =50, c = 'red')
plt.show()

For each of the scatters, I want to add data label 'date' to it. So I made these changes:

fig, ax = plt.subplots()
ax.scatter(dates, PM_25)
for i, txt in enumerate(dates):
    ax.annotate(txt, i)

enter image description here

It doesn't work.

What's the right way to label them? Thank you.

Upvotes: 0

Views: 507

Answers (1)

Henry Yik
Henry Yik

Reputation: 22503

You need both x and y when you annotate.

for i, txt in enumerate(dates):
    ax.annotate(txt, (dates[i],PM_25[i]))

Upvotes: 1

Related Questions