Basic Plotly Question about Reading from CSV

Hello All,

I have a pretty basic plotly question,

I am trying to read in some data from a CSV and then plot it using the line plot. Now I can read the values in question to a dataframe just fine, however I run into some issues when plotting them however.

My first issue is I don’t know what the column names will be, in all the examples of Plotly I have found it seems that there is knowledge beforehand of what column name will be, like for example in the generic example on the site you know the names of the columns you are plotting are going to be x and y, but I dont know what mine will be called to assign them. How do I get around this issue?

Let me add some context,

Here is the code I am more or less working with:

import csv
import numpy as np
import matplotlib.pyplot as plt
import sys
import re
import pandas as pd
import plotly as ex
import plotly.express as px
import plotly.graph_objects as go

df = pd.read_csv(file_name)
print(df.head())

x = df.iloc[:, [0]]
y = df.iloc[:, [1]]
col_mapping = [f"{c[0]}:{c[1]}" for c in enumerate(df.columns)]

##PLOT TABLE
fig = go.Figure(data=[go.Table(
header=dict(values=list(df.columns),
fill_color=‘paleturquoise’,
align=‘left’),
cells=dict(values=[x, y],
fill_color=‘lavender’,
align=‘left’))
])
fig.show()

fig = px.line(x, y, title=‘X versus Y’)
fig.show()


Now I’ve gotten the table to work pretty seamless, and I can get the graph to work if I hardcode the values, say like fig = px.lin (x = df.x and y = df.y, title "X versus Y) my problem is I don’t know what x and y will be called, like I it might be df.Time for example, so how do I make it generic? I tried using the iloc method and it works for the table, but when I pass those into the line graph it wont work cause of the headers, and I’d like to use the dataframe if possible. Thanks!

Hi, this is more a python question that plotly question. Basically, what you can do is to create your own column names. For example:

This is my .csv file

Time,             Col 1,            Col 2,              Col 3
0.0000,          6.046490,        1.695220000,       0.032136700
0.0005,          6.091780,        1.695220000,       0.032136700
0.0010,          6.137070,        1.711050000,       0.032136700

when you read your csv with pandas you can re-name the columns as follows:

df = pd.read_csv(filename.csv)
df.columns = ["Relative Time","channel 1","channel 2","channel 3"]

and then get those values as follows:

print(df['Relative Time'])

will generate:

[0.0000,          
0.0005,          
0.0010]

so in plotly you will know which column represents time and which represents another value. You don’t need to know the name, just to know how many columns has the csv file and what those columns represents.

1 Like

Thanks! I am relatively new to Python, I figured there might be a more elegant way to do it in using the dataframe object other then renaming, but this will work!

@Mehran,

Another solution, whithout column renaming, is as follows:

my_df= df.iloc[: , [0, 1]].copy() 
fig1 = px.line(my_df, x=my_df.columns[0], y=my_df.columns[1])
fig1.show() 
1 Like

ahh this is closer to what i was trying to do, and I could probably make it 0, n and have it take as many columns as comes in! This is great!