我有一个像这样的python程序:
import matplotlib.pyplot as plt
p = []
for i in range(1000):
p.append(i)
plt.plot(p)
p
是将在每次迭代中增长的数字列表。所以我喜欢在每次迭代中看到绘制结果,但是它显示了1000次迭代后的结果!我该如何解决?
编辑:
我尝试编辑代码并使用建议的解决方案,但是没有用,我的代码像以前一样运行:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import requests
import numpy as np
ask_volumes = []
bid_volumes = []
first_price = []
last_prices = []
fig, ax = plt.subplots()
y = []
for i in range(2600):
time.sleep(1)
codes = ['OSFKH98', 'OSFOR98','OSFTR98', 'SAFKH98','SAFOR98','SAFTR98']
all_data = []
with requests.Session() as s:
for code in codes:
data = {'ContractCode' : code}
r = s.post('http://cdn.ime.co.ir/Services/Fut_Live_Loc_Service.asmx/GetContractInfo', json = data ).json()
#print(r)
for key, value in r.items():
print(r[key]['ContractCode'])
ask_volumes.append( (r[key]['AskVolume1']) + (r[key]['AskVolume2']) + (r[key]['AskVolume3']) + (r[key]['AskVolume4']) + (r[key]['AskVolume5']) )
bid_volumes.append( (r[key]['BidVolume1']) + (r[key]['BidVolume2']) + (r[key]['BidVolume3']) + (r[key]['BidVolume4']) + (r[key]['BidVolume5']) )
first_price = (r[key]['FirstTradedPrice'])
last_prices = (r[key]['LastTradedPrice'])
y.append(last_prices)
x = np.arange(0,i+1,1)
line, = ax.plot(x, y)
def animate(num, x, y, line):
line.set_data(x[:num], y[:num])
return line,
ani = animation.FuncAnimation(fig, animate, fargs=[x, y, line],
interval=1, blit=True)
plt.show()
答案 0 :(得分:1)
像这样吗?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x = np.arange(0, 1000, 1)
y = x
fig, ax = plt.subplots()
line, = ax.plot(x, y)
def animate(num, x, y, line):
line.set_data(x[:num], y[:num])
return line,
ani = animation.FuncAnimation(fig, animate, fargs=[x, y, line],
interval=1, blit=True)
plt.show()
答案 1 :(得分:1)
基于对this问题的回答:
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
plt.show()
p = []
for i in range(10):
p+=[i]
plt.plot(p)
plt.pause(0.5)
对我来说,它运作良好,希望对您有所帮助。