Issue
I need to plot the data from csv file. yvalues are not in order thats why i need to sort them. I rotate the x axis because the strings are so long. Ho can I do?
####My data:####
####Code:#####
x = []
y = []
with open('output.csv','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(row[1])
y.append(row[2])
plt.plot(x[1:],y[1:], label='Loaded from file!')
plt.xlabel('x')
plt.xticks(rotation=90)
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()
Solution
EDIT:
It seems problem is simpler then I expected and solution is
y[1:] = sorted(y[1:])
Old answer
You should keep x
and y
as pairs on one list - like this
data = [ [x1, y1], [x2, y2], ... ]
so you could read it as
data = list(plots)
and then you can sort it
data.sort(key=lambda i:i[2])
or
data = sorted(data, key=lambda i:i[2])
And after that you should split data
x = [i[1] for i in data]
y = [i[2] for i in data]
OR you should read it to pandas.DataFrame
import pandas as pd
data = pd.read_csv('output.csv')
and then you can sort
data = data.sort_values(by='TotalTime')
and plot it without spliting
plt.plot(data['Name'], data['TotalTime']
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.