-1

I am trying to add data value to the end of the line of matplotlib. This is my code:

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

This is result: enter image description here

However, I want to add data value to the end of each line.

Also, which library that I can visualise data that support mouse hover like this: enter image description here

Any thoughts are appreciated.

MikeB
  • 37
  • 7

1 Answers1

0

You should be able to add a day to the date using timedelta(days=1) (from datetime library) in the manner described here: Adding 5 days to a date in Python.

Thus

y = line.get_ydata()[-1]
x = line.get_xdata()[-1]+timedelta(days=1) # or something reasonable
ax.annotate('{:d}'.format(y), xy=(x,y)  ...

For hover, you can use plotly or bokeh, the latter of which is probably easier.

jpf
  • 1,407
  • 11
  • 20
  • Hi jgf, thanks for your comment. However, I want to add data value (number of cases) to the end of each line. – MikeB Mar 30 '20 at 00:21
  • @MikeB isn't the data value just `y`? So instead of `name` you can annotate `y` formatted as string? By "line" you mean the line of data, right? Not some other line. – jpf Mar 30 '20 at 11:47
  • you are right. I did change to y but some labels just overlap so I changed to Plotly . BTW, thanks you. – MikeB Mar 30 '20 at 12:32