我正在尝试绘制5分钟的历史烛台,我遇到了这些问题:使用每日烛台数据进行绘制,它显示了正确的结果:1-day chart,但是对于5m,它看起来像这样:5 minutes chart
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(mpl_dates.date2num)
df = df.astype(float)
plt.style.use('dark_background')
levels = self.getLevels()
ax = plt.subplot2grid((1, 1), (0, 0), rowspan=6, colspan=4)
candlestick_ohlc(ax,df.values,width=0.4, \
colorup='green', colordown='red', alpha=1.0)
#ax.grid(True)
# Setting labels
ax.set_xlabel('Date')
ax.set_ylabel('Price')
# Formatting Date
date_format = mpl_dates.DateFormatter("%d/%m/%y %H:%M")
ax.xaxis_date()
ax.xaxis.set_major_formatter(date_format)
#fig.autofmt_xdate()
#fig.tight_layout()
plt.xticks(rotation=90)
for level in levels:
plt.hlines(level[1],xmin=df['date'][level[0]],\
xmax=max(df['date']),colors='blue')
plt.show()
发布于 2021-10-31 05:46:34
我运行了你的代码,但它没有像预期的那样工作。目前,mpl_finance已被弃用,建议使用mplfinance。mplfinance具有绘制趋势线等功能,并且可以绘制部分水平线。要绘制一条中断的线,请将起点和终点放在列表中并处理它。在我的示例中,我列出了起步价和高价,并将它们设置为行。有关详细的explanation,请参阅此页面。
import mplfinance as mpf
import matplotlib.pyplot as plt
import yfinance as yf
import pandas as pd
df = yf.download("AAPL", start="2021-10-25", end="2021-10-29", interval='5m', progress=False)
df.index = pd.to_datetime(df.index)
df.index = df.index.tz_localize(None)
df = df[:200] # Limit data to emphasize the lines in the graph.
o_lines = []
m_lines = []
for c in df.columns[:4]:
if c == 'Open':
idx, price1, end, price2 = df[c].head(1).index[0], df[c].head(1)[0], df[c].tail(1).index[0], df[c].head(1)[0]
o_lines.append((idx, price1))
o_lines.append((end, price2))
if c == 'High':
idx, price1, end, price2 = df.loc[df[c].idxmax()].name, df.loc[df[c].idxmax()][c], df[c].tail(1).index[0], df.loc[df[c].idxmax()][c]
m_lines.append((idx, price1))
m_lines.append((end, price2))
# print(m_lines)
mpf.plot(df, type='candle', alines=dict(alines=[o_lines, m_lines], colors=['b','r'], linewidths=2, alpha=0.4), style='yahoo')
发布于 2021-11-05 23:55:09
lines = []
for level in levels:
idx, price1, end, price2 = ohlc['date'][level[0]], level[1], max(ohlc['date']),level[1]
lines.append((idx, price1,end, price2))
print(lines)
mpf.plot(ohlc, type='candle',alines=dict(alines=lines,colors = 'y',linewidths=2, alpha=0.3), volume=True,panel_ratios=(4,1),addplot=indicators)
(
Timestamp("2021-10-01 20:15:00+0000", tz="UTC"),
31.58,
Timestamp("2021-10-03 21:00:00+0000", tz="UTC"),
31.58,
),
(
Timestamp("2021-10-01 22:15:00+0000", tz="UTC"),
32.29,
Timestamp("2021-10-03 21:00:00+0000", tz="UTC"),
32.29,
),
(
Timestamp("2021-10-02 01:15:00+0000", tz="UTC"),
32.72,
Timestamp("2021-10-03 21:00:00+0000", tz="UTC"),
32.72,
),
(
Timestamp("2021-10-02 02:00:00+0000", tz="UTC"),
32.0,
Timestamp("2021-10-03 21:00:00+0000", tz="UTC"),
32.0,
),
(
Timestamp("2021-10-02 22:15:00+0000", tz="UTC"),
33.02,
Timestamp("2021-10-03 21:00:00+0000", tz="UTC"),
33.02,
),
]
https://stackoverflow.com/questions/69781509
复制相似问题