Python make chart from input list

I have this working function that takes 2 inputs as .csv files and shows chart of the data.
What I would like to do is to turn function input into list instead of 2 files.

This is the function:

def Graph(first_file,second_file):
 
    fig = make_subplots(rows=1, cols=3)
    list_of_atributes = ["Total", "Clean", "Dirty"]
    data = pd.read_csv(first_file)
    data2= pd.read_csv(second_file)
    df = pandas.DataFrame(data)
    df2 = pandas.DataFrame(data2)
    data_list = []
    data_list2 = []
    show_bar = df[df.Name == "SUM"]
    show_bar2 = df2[df2.Name == "SUM"]
    for n in list_of_atributes:
        data_list.append(int(show_bar[n]))
        data_list2.append(int(show_bar2[n]))

    fig.add_trace(go.Bar(name='File 1', x=list_of_atributes, y=data_list, marker=dict(color='red')), row=1, col=2)
    fig.add_trace(go.Bar(name='File 2', x=list_of_atributes, y=data_list2 ,marker=dict(color='blue')), row=1, col=1)

    fig.show()
Grapfh("file1.csv", "file2.csv")

>Solution :

If I understand correctly, you want your function to plot things based on a list of files, so from 1 to n files, not specifically 2.

You could try this :

def Graph(files):
 
    fig = make_subplots(rows=1, cols=len(files)+1)

    for file_idx in range(len(files)) :

        list_of_atributes = ["Total", "Clean", "Dirty"]
        data = pd.read_csv(files[file_idx])
        df = pandas.DataFrame(data)
        data_list = []
    
        show_bar = df[df.Name == "SUM"] 
        for n in list_of_atributes:
           data_list.append(int(show_bar[n]))
    

        fig.add_trace(go.Bar(name=f'File {file_idx+1}', x=list_of_atributes, y=data_list, marker=dict(color='red')), row=1, col=file_idx+1)

    fig.show()
Graph(["file1.csv", "file2.csv"] )

Leave a Reply