简体   繁体   English

如何使用 mplfinance.plot() 或任何类似的 package 在每根蜡烛上方添加字符串注释?

[英]How to add a string comment above every single candle using mplfinance.plot() or any similar package?

我希望它是这样的图片

i want to add a string Comment above every single candle using mplfinance package.我想使用 mplfinance package 在每根蜡烛上方添加一个字符串评论。

is there a way to do it using mplfinance or any other package?有没有办法使用 mplfinance 或任何其他 package 来做到这一点?

here is the code i used:这是我使用的代码:

import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation
from mplfinance import *
import datetime
from datetime import date, datetime

fig = mpf.figure(style="charles",figsize=(7,8))
ax1 = fig.add_subplot(1,1,1 , title='ETH')

def animate(ival):
    idf = pd.read_csv("test1.csv", index_col=0)
    idf['minute'] = pd.to_datetime(idf['minute'], format="%m/%d/%Y %H:%M")

    idf.set_index('minute', inplace=True)

    ax1.clear()
    mpf.plot(idf, ax=ax1, type='candle',  ylabel='Price US$')

ani = animation.FuncAnimation(fig, animate, interval=250)

mpf.show()

You should be able to do this using Axes.text()您应该可以使用Axes.text()执行此操作

After calling mpf.plot() then call调用mpf.plot mpf.plot()然后调用

ax1.text()

for each text that you want (in your case for each candle).对于您想要的每个文本(在您的情况下为每个蜡烛)。

There is an important caveat regarding the x-axis values that you pass into ax1.text() :关于传递给ax1.text()x 轴值有一个重要的警告

  • If you do not specify show_nontrading=True then it will default to False in which case the x-axis value that you pass into ax1.text() for the position of the text must be the row number corresponding to the candle where you want the text counting from 0 for the first row in your DataFrame.如果您指定show_nontrading=True那么它将默认为False在这种情况下,您传递给文本ax1.text()x 轴值必须是与您想要的蜡烛相对应的行号DataFrame 中第一行的文本0开始计数。
  • On the other hand if you do set show_nontrading=True then the x-axis value that you pass into ax1.text() will need to be the matplotlib datetime .另一方面,如果您确实设置show_nontrading=True ,那么您传递给ax1.text()的 x 轴值将需要是matplotlib datetime You can convert pandas datetimes from you DataFrame DatetimeIndex into matplotlib datetimes as follows:您可以将pandas 日期时间从 DataFrame DatetimeIndex 转换为 matplotlib 日期时间,如下所示:
import matplotlib.dates as mdates
my_mpldates = mdates.date2num(idf.index.to_pydatetime())

I suggest using the first option (DataFrame row number) because it is simpler.我建议使用第一个选项(DataFrame 行号),因为它更简单。 I am currently working on an mplfinance enhancement that will allow you to enter the x-axis values as any type of datetime object (which is the more intuitive way to do it) however it may be another month or two until that enhancement is complete, as it is not trivial.我目前正在研究一种 mplfinance 增强功能,它允许您将 x 轴值输入为任何类型的日期时间 object(这是更直观的方法)但是它可能需要一两个月才能完成增强,因为它不是微不足道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM