Line Graph Creation from CSV File

I am attempting to create a graph from a csv file using pandas and plotly. I have got most of it working, but can’t seem to add a title to the right hand legend. Each time I add the color=‘something’ I get an error:

Traceback (most recent call last):
  File "C:\Users\c678014\py-progs\graph.py", line 7, in <module>
    fig = px.line(df, x = 'Date', y = ['Download', 'Upload' ], color = 'Throughput', title='ALB DWI Performance')
  File "C:\Users\c678014\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_chart_types.py", line 252, in line
    return make_figure(args=locals(), constructor=go.Scatter)
  File "C:\Users\c678014\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_core.py", line 1861, in make_figure
    args = build_dataframe(args, constructor)
  File "C:\Users\c678014\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_core.py", line 1377, in build_dataframe
    df_output, wide_id_vars = process_args_into_dataframe(
  File "C:\Users\c678014\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_core.py", line 1183, in process_args_into_dataframe
    raise ValueError(err_msg)
ValueError: Value of 'color' is not the name of a column in 'data_frame'. Expected one of ['index', 'Date', 'Download', 'Upload', 'Ping', 'Jitter'] but received: Throughput

Here is my code that does not work:

import pandas as pd
import plotly.express as px

header_list = [ "Date", "Download", "Upload", "Ping" , "Jitter" ]
df = pd.read_csv('20210121-dwi-haigs-https.csv', names=header_list).reset_index()

fig = px.line(df, x = 'Date', y = ['Download', 'Upload' ], color = 'Throughput', title='ALB DWI Performance')

fig.update_layout(
 xaxis_title='Time',
 yaxis_title='Mbps'
                  )
fig.show()

Here is the code that almost works:

import pandas as pd
import plotly.express as px

header_list = [ "Date", "Download", "Upload", "Ping" , "Jitter" ]
df = pd.read_csv('20210121-dwi-haigs-https.csv', names=header_list).reset_index()

fig = px.line(df, x = 'Date', y = ['Download', 'Upload' ],  title='ALB DWI Performance')

fig.update_layout(
 xaxis_title='Time',
 yaxis_title='Mbps'
                  )
fig.show()

Any help would be most appreciated

Pat

Hi Pat!

In plotly.express, color has to be a column of your pandas dataframe df. In this case, you could choose something like color="Jitter" if you want to color your graph using the Jitter column. Since you don’t have a Throughput column in df it creates an error.

Thanks for the information. Is there a way in plotly rather than plotly.express to achieve what I am trying to do?

Hi xhlu,

I have another question related to uploading multiple CSV files simultaneously to Dash and then separated each CSV file to each separate dataframe. In what order, do these files get uploaded? Does it go by Alphabet order or by the length of the filename or by the size of the file? Does the order of file that I clicked on inside the folder dictate the order of files got uploaded to Dash?

I found it not very reliable to upload multiple files to Dash. I have to load the set of multiple files several time before I figured out the order. This is not efficient coding for the different set of files. In addition, I found the order changes somehow. Would you please give me some advice on this?

The example you shared in the original post uses plotly.express.

Unfortunately I’m not very familiar with CSV uploading, so I can’t think of an easy solution for this. One thing I could suggest is to create a dictionary that maps the name of each file to the content of the file (which should be a byte string). Then you could sort the keys and process each file using the sorted order of the keys.

I like the dictionary idea. I guess it will help load the correct files regardless of which order the files are uploaded into the app. Thanks for taking your time to help me with my question.

I still want to learn more about the order of uploading multiple CSV files. Is there anyone that you would suggest me to contact with this question? I would really appreciate your help.

Thanks

1 Like