python - 如何从雅虎财经获取日期
问题描述
我在将 yfinance 中的日期放入我的 matplotlib 图表时遇到问题,有人可以帮助/告诉我如何将 yfinance 中的日期放入我的 matplotlib 图表中
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
# function getting data for the last x years with x weeks space from checking data and specific
observation.
def stock_data(ticker, period, interval, observation):
ticker = yf.Ticker(ticker)
ticker_history = ticker.history(period, interval)
print(ticker_history[observation]) #is here to show you the data that we get
date = pd.date_range(ticker_history[period])
x = date
y = np.array(ticker_history[observation])
plt.style.use('dark_background')
plt.plot(x,y)
plt.ylabel('Price($)')
plt.xlabel('Date', rotation=0)
plt.show()
stock_data('GOOGL', '6mo', '1wk', 'Open')
解决方案
已测试✅</p>
您可以从 It is a Pandas Series 对象中提取日期ticker_history[observation]
,所以我会这样做:
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
# function getting data for the last x years with x weeks space
# from checking data and specific observation.
def stock_data(ticker, period, interval, observation):
ticker = yf.Ticker(ticker)
ticker_history = ticker.history(period, interval)
print((ticker_history[observation]))
sf = ticker_history[observation]
df = pd.DataFrame({'Date':sf.index, 'Values':sf.values})
x = df['Date'].tolist()
y = df['Values'].tolist()
plt.style.use('dark_background')
plt.plot(x,y)
plt.ylabel('Price($)')
plt.xlabel('Date', rotation=0)
plt.show()
if __name__ == '__main__':
stock_data('GOOGL', '6mo', '1wk', 'Open')
推荐阅读
- mysql - Mysql安装配置在“启动服务器”时停止
- java - NoSuchBeanDefinitionException:没有名为“authService”的 bean 可用
- c# - 从 SourceTree 签出 git 分支后,在项目(解决方案中的多个项目)上加载失败错误
- ios - XCode 11.5 升级后 React-Native Image 不显示
- python - 如何提高 LSTM 模型的预测和准确性?
- javascript - 我通过函数 $.ajax() 从 javasript 发送到我的 http 控制器函数的 json 字符串为空
- python-3.x - Flask-migrate 未检测到架构更改
- google-apps-script - 如何自动从查看模式切换到编辑模式,反之亦然
- flutter - 对话框打开有延迟,在生成对话框之前导航到另一个视图时出现错误。我怎样才能让它不被生成?
- postgresql - Postgres VACUUM 和复制