gpt4 book ai didi

python - 如何使用 pyplot 在线绘图?

转载 作者:行者123 更新时间:2023-12-01 00:58:02 25 4
gpt4 key购买 nike

我有一个像这样的Python程序:

import matplotlib.pyplot as plt

p = []
for i in range(1000):
p.append(i)
plt.plot(p)

p 是在每次迭代中都会增长的数字列表。所以我喜欢在每次迭代中看到绘图结果,但它显示的是 1000 次迭代后的结果!我该如何修复它?

EDIT:

我尝试编辑我的代码并使用建议的解决方案,但没有成功,我的代码像以前一样运行:

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()

最佳答案

基于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)

对我来说效果很好,希望有帮助。

关于python - 如何使用 pyplot 在线绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56087708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com