在行matplotlib的末尾注释数据值

问题描述 投票:0回答:1

我正在尝试将数据值添加到matplotlib行的末尾。这是我的代码:

fig, ax = plt.subplots(1, 1)
anz_df.plot(legend=True, ax=ax, figsize= (16,8))
ax.set_title("Number of confirmed cases between Australian' States vs New Zealand")
ax.set_ylabel('Number of confirmed Cases')
ax.set_xlabel('Date')
for line, name in zip(ax.lines, anz_df.columns):
    y = line.get_ydata()[-1]
    ax.annotate(name, xy=(1,y), xytext=(6,0), color=line.get_color(), 
                xycoords = ax.get_yaxis_transform(), textcoords="offset points",
                size=14, va="center")

这是结果:enter image description here

但是,我想在每行的末尾添加数据值。

此外,我可以可视化哪个库来支持鼠标悬停的数据,如下所示:enter image description here

任何想法都值得赞赏。

python matplotlib annotate
1个回答
0
投票

您应该可以按timedelta(days=1)所述的方式使用Adding 5 days to a date in Python在日期中添加一天。

因此

y = line.get_ydata()[-1]
x = line.get_xdata()[-1]+timedelta(days=1) # or something reasonable
ax.annotate(name, xy=(x,y)  ...

对于悬停,可以使用绘图或散景,后者可能更容易。

© www.soinside.com 2019 - 2024. All rights reserved.