[Python] Trouble Plotting Data

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to Python. I am reading my adc data from file and generating 1-Dimensional numpy array. I have 1800 data over the period of 3 minutes so my sampling frequency is 10Hz. I want to plot this data but i am getting error

    raise ValueError(f"x and y must have same first dimension, but "
    f"have shapes {x.shape} and {y.shape}")


    below is my code

    import matplotlib.pyplot as plt
    import numpy as np

    arr1 = np.array

    file_path = "MyValues.log" # Replace with the actual path to your file

    fs = 10
    T = 180.0 # seconds
    n = int(T * fs) # total number of samples

    try:

    with open(file_path, 'r') as file:

    for line in file:
    # Each 'line' variable will contain one line from the file
    # including the trailing newline character ('\n') if present.
    # You can process the line here, for example, print it:
    #print(str(counter) + " - " + line.strip()) # .strip() removes leading/trailing whitespace, including '\n'
    arr1 = np.append(arr1, int(line.strip()))

    except FileNotFoundError:
    print(f"Error: The file '{file_path}' was not found.")
    except Exception as e:
    print(f"An error occurred: {e}")

    print(len(arr1))

    plt.figure(figsize=(10, 6)) # Set the figure size
    plt.plot(n, arr1, marker='o', linestyle='-', color='purple')
    plt.title('Plot of 1D Array Data')
    plt.xlabel('Index')
    plt.ylabel('Value')
    plt.grid(True)
    plt.xticks(n) # Ensure x-axis ticks align with indices
    plt.show()


    Any help please.

    Continue reading...

Compartilhe esta Página