2017-06-16 75 views
1

是否有可能讓一個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是時間),並在這個圖上實時繪製它們?

在此先感謝!

+0

你可以改變第一個文件的代碼,或者你試圖用t從它的'print(y)'語句捕獲輸出?它是否必須是單獨的文件,或者您是否只需向劇情程序添加代碼? – Prune

回答

0

你可以簡單地做一些與此類似:

import random_Y_file 
values = random_Y_file.getValues() 
print(values) 

在random_Y_files:

def getValues(): 
    return [randint(0, 1000) for _ in range(100)] # Returns 100 numbers from 0 to 1000 

要確保你正在使用你的Python文件是在同一文件夾中random_Y_file .py

+0

但是,我做到了,但仍然無法正常工作。我打印的是座標,但它們不是繪製在圖上,圖形是空白的。我修改了上面的代碼來顯示我更新的內容。 – sss

+0

對不起,沒有回答。 你確定它在圖中不是問題嗎? –

+0

沒有更多的問題!謝謝 :) – sss