如何从雅虎财经获取日期
how to get dates from yahoo finance
我在从 yfinance 获取日期到我的 matplotlib 图表时遇到问题有人可以 help/show 我如何从 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')
已测试 ✅
您可以从 ticker_history[observation]
中提取日期
它是一个 Pandas 系列对象,所以我会这样做:
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')
我在从 yfinance 获取日期到我的 matplotlib 图表时遇到问题有人可以 help/show 我如何从 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')
已测试 ✅
您可以从 ticker_history[observation]
中提取日期
它是一个 Pandas 系列对象,所以我会这样做:
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')