我從網上借了一些代碼來制作這個折線圖影片,但不幸的是,它帶有某種用于 x 軸的 count() 而不是我從雅虎匯入的日期。我是新手,你能幫忙嗎?
我得到了一個不錯的情節,但我寧愿在 x 軸下有日期。
import pandas_datareader.data as web
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
from pylab import legend
from matplotlib import animation
from matplotlib.animation import FuncAnimation
from itertools import count
plt.style.use('ggplot')
fig, axes = plt.subplots(nrows = 1, ncols = 1, figsize = (12,5))
# set up investment choice and start/end dates
choice = str(input('Please enter your investment symbol: '))
start = str(input('Please enter your comparison starting date(mm/dd/yyyy):'))
end = dt.date.today()
symbol = '^GSPC', choice #S&P500 symbol and your choice for comparison
# source of data and method of acqusition of data
source = 'yahoo'
data = web.DataReader(symbol, source, start, end)['Adj Close']
#adjust data shares for even start:
numer = data.iat[0,0]
denom = data.iat[0,1] #call starting date value for chosen symbol
shares = numer/denom #compute number of shares for even start
data[choice] = shares*(pd.DataFrame(data[choice])) #Shares*share price
#for entire choice column
#for comparisons
for i in range(0, len(data)): #set up animation steps
l1 = data['^GSPC']
l2 = data[choice]
x1,y1,y2,y3 = [], [], [], []
xval = count(0,1.37)
print(l1, l2)
def animate(i):
x1.append(next(xval))
y1.append((l1[i]))
y2.append((l2[i]))
axes.plot(x1,y1, color="red")
axes.plot(x1,y2, color="blue")
if __name__ == '__main__':
try:
anim = FuncAnimation(fig, animate, interval=1)
#plt.plot(data)
plt.xlabel('Days')
plt.ylabel('Dollars')
plt.title('The S&P500 (red) vs. Your Investment (blue),'
' Using Adjusted Share Amounts for Even Start')
#legend(['S&P500', choice])
plt.show()
except IndexError as e:
print(e)
print(sys.exc_type)
uj5u.com熱心網友回復:
您的代碼是否因錯誤而失敗?我發生了這種情況。我洗掉了額外的函式呼叫 def init()。
import pandas_datareader.data as web
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import sys
plt.style.use('ggplot')
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(12, 5))
# set up investment choice and start/end dates
choice = str(input('Please enter your investment symbol: '))
start = str(input('Please enter your comparison starting date(mm/dd/yyyy):'))
end = dt.date.today()
symbol = '^GSPC', choice # S&P500 symbol and your choice for comparison
# source of data and method of acqusition of data
source = 'yahoo'
data = web.DataReader(symbol, source, start, end)['Adj Close']
# adjust data shares for even start:
numer = data.iat[0, 0]
denom = data.iat[0, 1] # call starting date value for chosen symbol
shares = numer / denom # compute number of shares for even start
data[choice] = shares * (pd.DataFrame(data[choice])) # Shares*share price
x1, y1, y2, y3 = [], [], [], []
def init():
pass
def animate(i):
if (len(data) - 1) == i:
anim.event_source.stop()
x1.append(data['^GSPC'].index[i])
y1.append((data['^GSPC'].values[i]))
y2.append((data[choice].values[i]))
axes.plot(x1, y1, color="red")
axes.plot(x1, y2, color="blue")
if __name__ == '__main__':
try:
anim = FuncAnimation(fig, animate, init_func=init, interval=1)
# plt.plot(data)
plt.xlabel('Days')
plt.ylabel('Dollars')
plt.title('The S&P500 (red) vs. Your Investment (blue),'
' Using Adjusted Share Amounts for Even Start')
# legend(['S&P500', choice])
plt.show()
except IndexError as e:
print(e)
print(sys.exc_type)
如果你不需要影片,那么下面的代碼是沒有影片的。稍微改變了你的代碼。洗掉了明顯不必要的陣列創建:x1,y1,y2,y3。
import pandas_datareader.data as web
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
plt.style.use('ggplot')
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(12, 5))
# set up investment choice and start/end dates
choice = str(input('Please enter your investment symbol: '))
start = str(input('Please enter your comparison starting date(mm/dd/yyyy):'))
end = dt.date.today()
symbol = '^GSPC', choice # S&P500 symbol and your choice for comparison
# source of data and method of acqusition of data
source = 'yahoo'
data = web.DataReader(symbol, source, start, end)['Adj Close']
numer = data.iat[0, 0]
denom = data.iat[0, 1] # call starting date value for chosen symbol
shares = numer / denom # compute number of shares for even start
data[choice] = shares * (pd.DataFrame(data[choice])) # Shares*share price
axes.plot(data['^GSPC'].index, data['^GSPC'], color="red")
axes.plot(data[choice].index, data[choice], color="blue")
plt.xlabel('Days')
plt.ylabel('Dollars')
plt.title('The S&P500 (red) vs. Your Investment (blue),' ' Using Adjusted Share Amounts for Even Start')
# legend(['S&P500', choice])
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449626.html
