Search code examples
pythoncsvmatplotlibgraph

How to plot data from .csv file which has the data from CAN communication(its receives data in 4 packets of 4 data points)


The format of the .csv file is as below it has gaps in between as it gets data in packets. I want to plot the data with timestamp on the x-axis and sensor1 on the y-axis using matplotlib in python so is there a possibility.

This is the data in the CSV file so you can see 4 data points received 4 times this is being read at different time stamps. I tried approaching the normal way but it shows a blank plot.

This is the link to the CSV file. https://docs.google.com/spreadsheets/d/17SIabIYYmSogOdeYTzpEwy9s2pZuVO3ghoChSSgGwAg/edit?usp=sharing

thanks in advance.

import pandas as pd

import matplotlib.pyplot as plt

data =  pd.read_csv("cantype.csv")

X = data.time_stamp

Y = data.sensor1

# plt.plot(data.time_stamp,data.BMS_01_CellVolt01)

plt.plot(X,Y)

plt.show()

Data

time_stamp,sensor1,sensor2,sensor3,sensor4,sensor5,sensor6,sensor7,sensor8,sensor9,sensor10,sensor11,sensor12,sensor13,sensor14,sensor15,sensor16
1.37E+12,1.50465,1.50405,1.50435,1.5042,,,,,,,,,,,,
1.37E+12,,,,,1.47105,1.5042,1.5045,1.50435,,,,,,,,
1.37E+12,,,,,,,,,1.49115,1.49205,1.4961,1.49865,,,,
1.37E+12,,,,,,,,,,,,,1.50405,1.5042,1.50405,1.50435
1.37E+12,1.50465,1.50405,1.50435,1.5042,,,,,,,,,,,,
1.37E+12,,,,,1.47105,1.5042,1.5045,1.50435,,,,,,,,
1.37E+12,,,,,,,,,1.49115,1.49205,1.4961,1.49865,,,,
1.37E+12,,,,,,,,,,,,,1.50405,1.5042,1.50405,1.50435

Solution

  • Load the csv with pandas:

    import pandas as pd
    df = pd.read_csv('cantype.csv')
    

    Then either use pandas plotting:

    df.plot(x='time_stamp', y='sensor1', marker='.')
    

    Or pure matplotlib:

    import matplotlib.pyplot as plt
    plt.plot(df.time_stamp, df.sensor1, marker='.')
    

    With your sample data, the plot does not look like a meaningful time series because there are only two (timestamp, sensor1) points and both are located at (1.37E+12, 1.50465):

    sample output