matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.55k stars 616 forks source link

Annotations #387

Open alvarozamora opened 3 years ago

alvarozamora commented 3 years ago

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

DanielGoldfarb commented 3 years ago

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 commented 2 years ago

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