Reputation: 43
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:
Graph only showing one set of results:
Upvotes: 1
Views: 42
Reputation: 501
I think you can do:
plt.scatter(df1['Year'],df1['Long header here'])
plt.scatter(df2['Year'],df2['Long header here'])
plt.show()
Upvotes: 0
Reputation: 46759
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.
Upvotes: 1