sss
sss

Reputation: 59

Read an array from one python file and plot it using Matplotlib

I am looking to read the following array created with python that creates random numbers:

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

and plot it into another file using 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()

When I try to do this, I receive an error message stating that the name 'data' is not an attribute of that function. The problem area seems to be values= (RandomData.data). How do you plot this array from one python file into the other? It is printing the array just fine. It is not plotting the points, like it is needed to.

Upvotes: 1

Views: 127

Answers (1)

burketae
burketae

Reputation: 26

Debugged w/ OP offline, but for anyone curious: One change required in the random number generator thing:

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

this returns the data as an array of len 25, rather than an array of len 1 containing an array of len 25

then in the printy graphy thing, restructure the add / animate calls to iterate through like so:

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

Upvotes: 1

Related Questions