Search code examples
pythoncsv

How can I get data from two CSV files to show one on graph for comparison?


I've been trying to get data from two separate CSV files to show on one graph so that visual comparison of the data can be done. I can get both results to print once the script has run, but I can't get that data to show on the graph. I'm sure I'm missing something here but I've been stuck on this for a while.

Here is the source code:

import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model

df1 = pd.read_csv(r'/home/morgankemp/Downloads/Data2.csv')
df = pd.read_csv(r'/home/morgankemp/Downloads/Data.csv')
print (df)
print(df1)

X = list(df.iloc[:, 0])
Y = list(df.iloc[:, 1])

plt.scatter(X, Y, color='b')
plt.title("Levels of Methane (CH4) Emissions (Tonnes) From Denmark 2010 - 2019")
plt.xlabel("Years")
plt.ylabel("Number of Tonnes")

plt.show()

If someone could point me in the right direction, thanks.

Output of code:

enter image description here

Graph only showing one set of results:

enter image description here


Solution

  • You could try the following kind of approach to loop over each file and colour combination:

    import pandas as pd
    import matplotlib.pyplot as plt
    from sklearn import linear_model
    
    data = [
        [r'/home/morgankemp/Downloads/Data.csv', 'b'], 
        [r'/home/morgankemp/Downloads/Data2.csv', 'g']
    ]
    
    for filename, colour in data:
        df = pd.read_csv(filename)
        X = list(df.iloc[:, 0])
        Y = list(df.iloc[:, 1])
        plt.scatter(X, Y, color=colour)
    
    plt.title("Levels of Methane (CH4) Emissions (Tonnes) From Denmark 2010 - 2019")
    plt.xlabel("Years")
    plt.ylabel("Number of Tonnes")
    
    plt.show()
    

    Without the data in text format it is difficult to test.