Search code examples
pythondataframeplotly

How to combine sensor data for plotting


I'm testing a light sensor for sensitivity. I now have data that I would like to plot.

  • The sensor has 24 levels of sensitivity
  • I'm only testing 0,6,12,18 and 23
  • On the x-axes: PWM value, range 0-65000

My goal is to plot from a dataframe using with plotly.

My question is: How can I combine the data (as shown below) into a dataframe for plotting?

EDIT: The link to my csv files: https://filetransfer.io/data-package/QwzFzT8O

Also below: my code so far

Thanks!

enter image description here

def main_code():

    data = pd.DataFrame(columns=['PWM','sens_00','sens_06','sens_12','sens_18','sens_23'])
    sens_00 = pd.read_csv('sens_00.csv', sep=';')
    sens_06 = pd.read_csv('sens_06.csv', sep=';')
    sens_12 = pd.read_csv('sens_12.csv', sep=';')
    sens_18 = pd.read_csv('sens_18.csv', sep=';')
    sens_23 = pd.read_csv('sens_23.csv', sep=';')

    print(data)
    print(sens_23)


import plotly.express as px
import pandas as pd

if __name__ == '__main__':

    main_code()

Solution

  • Here is my suggestion. You have two columns in each file, and you need to use unique column names to keep both columns. All files are loaded and appended to the empty DataFrame called data. To generate a plot with all columns, you need to specify it by fig.add_scatter. The code:

    import pandas as pd
    import plotly.express as px
    
    
    def main_code():
        data = pd.DataFrame()
        for filename in ['sens_00', 'sens_06', 'sens_12', 'sens_18', 'sens_23']:
            data[['{}-PWM'.format(filename), '{}-LUX'.format(filename)]] = pd.read_csv('{}.csv'.format(filename), sep=';')
    
        print(data)
    
        fig = px.line(data_frame=data, x=data['sens_00-PWM'], y=data['sens_00-LUX'])
        for filename in ['sens_06', 'sens_12', 'sens_18', 'sens_23']:
            fig.add_scatter(x=data['{}-PWM'.format(filename)], y=data['{}-LUX'.format(filename)], mode='lines')
        fig.show()
    
    
    if __name__ == '__main__':
        main_code()