如何使用matplotlib和mpl\u finance从晨星数据生成烛台图?

2024-04-30 03:44:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从晨星导入数据并生成烛台图。你知道吗

import numpy as np
import pandas as pd
import pandas_datareader.data as web
import mpl_finance as mpf
import matplotlib.pyplot as plt


AAPL = web.DataReader('AAPL', 'morningstar', start = '1/1/2000',end =  '5/29/2018')


fig, ax = plt.subplots(figsize = (8,5))
fig.subplots_adjust(bottom=0.2)
mpf.candlestick_ohlc(ax, AAPL, width = 0.6, colorup = 'b', colordown = 'r', alpha = 0.75)
ax.xaxis_date()
ax.autoscale_view()
ax.xaxis.grid(True, 'major')
ax.grid(True)

我得到一个错误:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

非常感谢您的帮助


Tags: importwebtruepandasasfigpltax
1条回答
网友
1楼 · 发布于 2024-04-30 03:44:37

您可以尝试将数据转换为浮点:AAPL.astype(float),看起来里面可能有字符串而不是数字。你知道吗

另外,我认为你必须使用序数日期和mpl_finance candlestick_ohlc

    zip(mdates.date2num(AAPL.index.to_pydatetime()), AAPL['Open'], AAPL['High'], AAPL['Low'], AAPL['Close'], AAPL['Volume'])

然后在你的candlestick_ohlc中使用它,而不是AAPL。你知道吗

相关问题 更多 >