【问题标题】:Read an array from one python file and plot it using Matplotlib从一个 python 文件中读取一个数组并使用 Matplotlib 绘制它
【发布时间】:2017-06-20 00:43:12
【问题描述】:

我正在寻找使用 python 创建的以下数组,该数组创建随机数:

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

当我尝试执行此操作时,我收到一条错误消息,指出名称“数据”不是该函数的属性。问题区域似乎是 values= (RandomData.data)。你如何将这个数组从一个 python 文件绘制到另一个文件中?它正在打印数组就好了。它没有像需要的那样绘制点。

【问题讨论】:

  • RandomData你需要return data在最后,而不是在模块的最后调用RandomData()。然后,在主函数中,只需执行values = RandomData()
  • @GergesDib 我收到一条错误消息,指出 TypeError: float() 参数必须是字符串或数字,而不是“函数”。
  • 你在哪一行得到错误

标签: python arrays matplotlib plot


【解决方案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()

这会将数据作为长度为 25 的数组返回,而不是长度为 1 的数组包含长度为 25 的数组

然后在 printy graphy 中,重组 add / animate 调用以像这样迭代:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2019-02-05
    相关资源
    最近更新 更多