散点图以将日期添加为数据标签

时间:2019-10-17 06:08:34

标签: python matplotlib label scatter-plot

在下面使用散点图绘制

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

对于每个散点图,我想向其添加数据标签“日期”。所以我做了这些改变:

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

enter image description here

它不起作用。

标记它们的正确方法是什么?谢谢。

1 个答案:

答案 0 :(得分:1)

注释时,您同时需要xy

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