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:
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()
CodePudding user response:
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()
data[['sens_00-PWM', 'sens_00-LUX']] = pd.read_csv('sens_00.csv', sep=';')
data[['sens_06-PWM', 'sens_06-LUX']] = pd.read_csv('sens_06.csv', sep=';')
data[['sens_12-PWM', 'sens_12-LUX']] = pd.read_csv('sens_12.csv', sep=';')
data[['sens_18-PWM', 'sens_18-LUX']] = pd.read_csv('sens_18.csv', sep=';')
data[['sens_23-PWM', 'sens_23-LUX']] = pd.read_csv('sens_23.csv', sep=';')
print(data)
fig = px.line(data_frame=data, x=data['sens_00-PWM'], y=data['sens_00-LUX'])
fig.add_scatter(x=data['sens_06-PWM'], y=data['sens_06-LUX'], mode='lines')
fig.add_scatter(x=data['sens_12-PWM'], y=data['sens_12-LUX'], mode='lines')
fig.add_scatter(x=data['sens_18-PWM'], y=data['sens_18-LUX'], mode='lines')
fig.add_scatter(x=data['sens_23-PWM'], y=data['sens_23-LUX'], mode='lines')
fig.show()
if __name__ == '__main__':
main_code()
CodePudding user response:
Based on the suggestion by @Dawid
This is what I was going for.