Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"] )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading