使用yfinance获取数据后Plotly蜡烛图无显示的问题求助
yfinance获取数据后Plotly蜡烛图无显示的问题求助
我现在尝试用yfinance获取OHLCV数据,再用Plotly绘制一个简单的蜡烛图,但遇到了麻烦:数据能正常下载成功,可最终生成的图表上完全看不到任何蜡烛。
以下是我编写的代码:
import yfinance as yf import pandas as pd import plotly.graph_objects as go from datetime import datetime tf = '1d' # Time frame (daily) asset = 'AAPL' # Asset ticker (e.g., Apple) start = '2019-01-01' # Start date end = datetime.now().strftime('%Y-%m-%d') # End date is current date df = yf.download(asset, start=start, end=end, interval=tf) df['pct_chg'] = df['Close'].pct_change() * 100 df.index.name = 'timestamp' # now plot the chart hover_text = [f"Open: {open}<br>Close: {close}<br>Pct: {pct_chg:.2f}%" for open, close, pct_chg in zip(df['Open'], df['Close'], df['pct_chg'])] # Create a candlestick chart using Plotly fig = go.Figure(data=[go.Candlestick( x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], hovertext=hover_text, hoverinfo='text' )]) # Update layout fig.update_layout( title='Candlestick chart', xaxis_title='Date', yaxis_title='Price', xaxis_rangeslider_visible=False, template='plotly_dark') # Show the plot fig.show()
我已经确认数据是正确下载到的,但就是图表上没有蜡烛显示,有没有大佬能帮我排查下问题出在哪里?
备注:内容来源于stack exchange,提问作者soo
相关产品推荐
相关产品推荐

