gpt4 book ai didi

python - 使用 matplotlib 在烛台图表中叠加数据

转载 作者:行者123 更新时间:2023-12-05 01:32:04 25 4
gpt4 key购买 nike

我的第一个需要是在烛台图表上叠加一条曲线。要绘制此烛台图表,mplfinance 看起来非常好。不幸的是,我没有找到在烛台上叠加额外曲线的方法。

考虑下面的示例,如果你们中有人知道如何将“噪声”列覆盖在从“分钟”数据框绘制的烛台上,那就太好了!

import pandas as pd
import mplfinance as mpf

minutely = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),
pd.Timestamp('2021-01-07 00:01:00'),
pd.Timestamp('2021-01-07 00:02:00'),
pd.Timestamp('2021-01-07 00:03:00'),
pd.Timestamp('2021-01-07 00:04:00')],
'Open':[36769.36, 36880.00, 36851.42,36922.19,37083.18],
'High':[36880.00, 36880.00, 36950.00, 37089.69, 37094.70],
'Low': [36760.00, 36817.64, 36810.03, 36922.13, 36565.49],
'Close':[36880.00, 36851.97, 36922.14, 37075.80, 36691.3]})

noise = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),
pd.Timestamp('2021-01-07 00:01:00'),
pd.Timestamp('2021-01-07 00:02:00'),
pd.Timestamp('2021-01-07 00:03:00'),
pd.Timestamp('2021-01-07 00:04:00')],
'Noise':[36779.36, 36870.00, 36881.42,36902.19,37103.18]})

# Draw candlesticks
minutely = minutely.set_index('Date')
noise = noise.set_index('Date')
mpf.plot(minutely, type='candle')

不止于此,我想使用“旧”candlestick_ohlcv 函数绘制烛条图,然后使用原生 matplotlib 功能叠加数据。这给出了下面的代码,但是当显示烛台时,x 标度显得毫无意义。

我应该只有 5 根蜡烛,x 刻度从 2020/01/07 00:00 到 2020/01/07 00:04。

我可以看到图表显示了从 2020/01/06 16:48 到 2020/01/07 07:12 的 x 刻度。

我不明白这个,蜡烛毫无意义......

import pandas as pd
import matplotlib.pyplot as plt
from mplfinance.original_flavor import candlestick_ohlc
import matplotlib.dates as mpdates

minutely = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),
pd.Timestamp('2021-01-07 00:01:00'),
pd.Timestamp('2021-01-07 00:02:00'),
pd.Timestamp('2021-01-07 00:03:00'),
pd.Timestamp('2021-01-07 00:04:00')],
'Open':[36769.36, 36880.00, 36851.42,36922.19,37083.18],
'High':[36880.00, 36880.00, 36950.00, 37089.69, 37094.70],
'Low': [36760.00, 36817.64, 36810.03, 36922.13, 36565.49],
'Close':[36880.00, 36851.97, 36922.14, 37075.80, 36691.3]})

noise = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),
pd.Timestamp('2021-01-07 00:01:00'),
pd.Timestamp('2021-01-07 00:02:00'),
pd.Timestamp('2021-01-07 00:03:00'),
pd.Timestamp('2021-01-07 00:04:00')],
'Noise':[36779.36, 36870.00, 36881.42,36902.19,37103.18]})

minutely['Date'] = minutely['Date'].map(mpdates.date2num)

plt.style.use('dark_background')
fig, ax = plt.subplots()
candlestick_ohlc(ax, minutely.values, width = 0.6,
colorup = 'green', colordown = 'red',
alpha = 0.8)
# Setting labels
ax.set_xlabel('Date')
ax.set_ylabel('Price')
# Formatting Date
date_format = mpdates.DateFormatter('%d-%m-%Y %H:%M')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()
fig.tight_layout()
# show the plot
plt.show()

拜托,有人知道将此外部数据叠加到烛台数据的方法吗?在此先感谢您的帮助,最好的,

最佳答案

您可以通过在上面的第一个代码示例中添加仅一行代码来做到这一点:

在调用 mpf.plot() 之前添加以下行:

ap = mpf.make_addplot(noise)

然后更改对 mpf.plot() 的调用以包含 addplot 关键字,因此:

ap = mpf.make_addplot(noise)
mpf.plot(minutely, type='candle', addplot=ap)

enter image description here

如果需要,您还可以修改日期时间格式:

ap = mpf.make_addplot(noise)
mpf.plot(minutely, type='candle', addplot=ap, datetime_format='%b %d, %H:%M')

enter image description here

我强烈建议您通读这两个教程。它们相对较短,可能只需要 15 到 30 分钟就可以仔细阅读它们:


附言作为一般规则,我不鼓励访问 mplfinance 的 Figure 和 Axes 对象:您的代码因此会简单得多。对图和轴的访问只能用于需要 mplfinance 尚不支持的高级功能的绘图。如果您通读了这些教程,我相信您会发现大多数使用财务图表完成的事情都可以简单地完成,而无需直接借助图形和坐标轴

关于python - 使用 matplotlib 在烛台图表中叠加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65778125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com