mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

Annotations

Open alvarozamora opened this issue 3 years ago • 2 comments

How can I add annotations? e.g. plt.annotate(...)

alvarozamora avatar Apr 28 '21 02:04 alvarozamora

Alvaro, Mplfinance does not directly support annotations at this time. However you can gain access to mplfinance's Figure and Axes objects and call the appropriate matplotlib methods to annotate your plot.

I would, however, not recommend calling plt.annotate(...), nor any other plt...() method in general, but rather find the analogous method that can be called directly on a Figure or Axes object (for example Axes.annotate()).

I would also suggest that you read the comments in the links on this post to get additional ideas on how to implement annotations (such as call Axes.text(), etc.

Finally, one thing to keep in mind when using these workarounds: If mpf.plot() kwarg show_nontrading=False (i.e. it's default value), then although you see dates and/or times printed on the x-axis, under the hood the x-axis is actually the row number from your dataframe. Therefore, for those annotation apis (text, etc) that take x,y coordinates, the x-coordinate may need to be in units of the dataframe row that contains the particular date you want to specify. I hope that makes sense. I am currently working on an enhancement to provide a method to do this transformation for you. In the meantime, that's what you have to do when specifying x-axis positions. Alternatively set show_nontrading=True and pass in Matplotlib date objects as the x values.

All the best. --Daniel

DanielGoldfarb avatar Apr 28 '21 04:04 DanielGoldfarb

here is another example: https://github.com/matplotlib/mplfinance/issues/58#issuecomment-1023612635

DanielGoldfarb avatar Jan 27 '22 21:01 DanielGoldfarb