2017-06-20 30 views
1

我期待閱讀與Python創建下列陣列創建隨機數繪製它:閱讀從一個Python文件的數組,並使用Matplotlib

# Import the random module 
import random 
import numpy as np 

# Define main function 
def RandomData(): 
    # Create a for loop to continuously generate random numbers 
    # Set a range of 12000 random numbers to be generated 
    for count in range(1): 
     # State the y coordinate as a random integer between 0 and 1000 
     #y = random.randint(0,1000) 
     # Create an array 
     data = np.array ([(random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000)) 
       ]) 

     # Print y 
     print(data) 

# Call main function 
RandomData() 

,並用它繪製到另一個文件的Python:

# Import time, collections, matplotlib and Random_Generation_List modules 
import time 
from collections import deque 
from matplotlib import pyplot as plt 
from matplotlib import style 
from Random_Generation_List import RandomData 

# Identify start as time.time 
# References the time module imported 
start = time.time() 

# Create RealtimePlot Class for object template 
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) 
     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 

    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 

# Define the main function 
def main(): 

    # Use matplotlib style of dark background 
    style.use('dark_background') 

    # Dar the figure 
    fig, axes = plt.subplots() 
    # Display the axes 
    display = RealtimePlot(axes) 
    # Label the x axis 
    axes.set_xlabel("Seconds") 
    #Label the y axis 
    axes.set_ylabel("Amplitude") 
    # Read the y values generated from Random_Generation_List.py 
    values= (RandomData.data) 
    # Print out values 
    print(values) 
    display.animate(fig, lambda frame_index: (time.time() - start, values)) 
    plt.show() 

    while True: 
     display.add(time.time() - start, values) 
     plt.pause(0.001) 

# Call the main function 
if __name__ == "__main__": main() 

當我嘗試這樣做時,我收到一條錯誤消息,指出名稱'data'不是該函數的屬性。問題區域似乎是值=(RandomData.data)。你如何將這個數組從一個python文件繪製到另一個?它正在打印陣列就好了。它不會繪製點,就像它需要的那樣。

+1

在'Rando成mData',你最後需要'返回數據',而不是在模塊末尾調用'RandomData()'。然後,在主函數中,只需執行'values = RandomData()'。 –

+0

@GergesDib我收到一個錯誤,指出TypeError:float()參數必須是字符串或數字,而不是'function'。 – sss

+0

你會在哪一行發生錯誤 –

回答

1

調試的瓦特/ OP離線,但對於任何人好奇:

# Import the random module 
import random 
import numpy as np 

# Define main function 
def RandomData(): 
    # Create a for loop to continuously generate random numbers 
    # Set a range of 12000 random numbers to be generated 
    for count in range(1): 
     # State the y coordinate as a random integer between 0 and 1000 
     #y = random.randint(0,1000) 
     # Create an array 
     data = np.array ((random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000),random.randint(0,1000), 
          random.randint(0,1000),random.randint(0,1000),random.randint(0,1000)) 
       ) 

     # Print y 
     return data 

# Call main function 
RandomData() 

該返回數據作爲LEN 25的陣列,而不是LEN的數組:在隨機數發生器事需要 一個變化含有1爲len 25

陣列然後在printy造影的事情,重組該加載/調用動畫像這樣以遍歷:

# Import time, collections, matplotlib and Random_Generation_List modules 
import time 
from collections import deque 
from matplotlib import pyplot as plt 
from matplotlib import style 
from rdata import RandomData 

# Identify start as time.time 
# References the time module imported 
start = time.time() 

# Create RealtimePlot Class for object template 
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) 
     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 

    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 

# Define the main function 
def main(): 

    # Use matplotlib style of dark background 
    style.use('dark_background') 

    # Dar the figure 
    fig, axes = plt.subplots() 
    # Display the axes 
    display = RealtimePlot(axes) 
    # Label the x axis 
    axes.set_xlabel("Seconds") 
    #Label the y axis 
    axes.set_ylabel("Amplitude") 
    # Read the y values generated from Random_Generation_List.py 
    values=RandomData() 
    # Print out values 
    print(values) 
    for val in values: 
     display.animate(fig, lambda frame_index: (time.time() - start, val)) 
     display.add(time.time() - start, val) 
     plt.pause(0.001) 
    plt.show() 

# Call the main function 
if __name__ == "__main__": main()