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!