是否有可能讓一個python文件執行另一個python文件來隨機繪製y值來繪製到圖上?如果是這樣,一個人怎麼辦呢?Python文件生成要繪製在實時圖上的點
目前我有生成隨機y值的列表的文件:
import random
import threading
def main():
for count in range(12000):
y = random.randint(0,10000)
print(y)
def getvalues():
return [random.randint(0, 1000) for count in range(12000)]
def coordinate():
threading.Timer(0.0001, coordinate).start()
coordinate()
main()
而且使用隨機值生成圖表的文件(式嵌入的):
import time
import random
from collections import deque
from matplotlib import pyplot as plt
from matplotlib import style
import Random_Generation_List
start = time.time()
class RealtimePlot:
def __init__(self, axes, max_entries = 100):
self.axis_x = deque(maxlen=max_entries)
self.axis_y = deque(maxlen=max_entries)
self.axes = axes
self.max_entries = max_entries
self.lineplot, = axes.plot([], [], "g-")
self.axes.set_autoscaley_on(True)
def add(self, x, y):
self.axis_x.append(x)
self.axis_y.append(y)
values = Random_Generation_List.getvalues()
self.lineplot.set_data(self.axis_x, self.axis_y)
self.axes.set_xlim(self.axis_x[0], self.axis_x[-1] + 1e-15)
self.axes.relim(); self.axes.autoscale_view() # rescale the y-axis
print(values)
def animate(self, figure, callback, interval = 50):
def wrapper(frame_index):
self.add(*callback(frame_index))
self.axes.relim(); self.axes.autoscale_view() # rescale the y-axis
return self.lineplot
def main():
style.use('dark_background')
fig, axes = plt.subplots()
display = RealtimePlot(axes)
axes.set_title("Real-time Plot")
axes.set_xlabel("Time")
axes.set_ylabel("Amplitude")
while True:
display.add(time.time() - start, random.random() * 100)
plt.pause(0.001)
display.animate(fig, lambda frame_index: (time.time() - start, random.random() * 100))
plt.show()
if __name__ == "__main__": main()
如何我結合這些文件並執行該文件以生成隨機數作爲y(x是時間),並在這個圖上實時繪製它們?
在此先感謝!
你可以改變第一個文件的代碼,或者你試圖用t從它的'print(y)'語句捕獲輸出?它是否必須是單獨的文件,或者您是否只需向劇情程序添加代碼? – Prune