Click here to Skip to main content
14,766,265 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a CSV file with stock data of companies along with opening, closing, high, etc. values for every day of two years. Now I have two lists, each containing the dates in order of both years. But when plotted in Matplotlib, it is not coming in order, the graph is messed up. When I have printed them normally, they are in order, but in Matplotlib graph figure they are not

What I have tried:

import csv
import codecs
import numpy as np
import matplotlib.pyplot as plt
# reading csv file
filepath = "sorted_nse_data.csv"
with codecs.open(filepath,"r") as csvfile:
    reader = csv.reader(csvfile)
    data = [row for row in reader]
    
x = np.shape(data)
i = 1
print(x)
date_train = []
openf = []
while(i<x[0]):
    if(data[i][0] == data[671][0] ):
        if(data[i][10] == '02-01-2017'):
            break
        date_train.append(str(data[i][10]))
        openf.append(data[i][3])
        
    i = i + 1
    
print(date_train)
train_data = openf
print(train_data)

while(i<x[0]):
    if(data[i][10] == '02-01-2017'):
        print (i)
        break
    i = i + 1
j = i   
test_data = []
date_test = []
while((j>=i) and (j<x[0])):
    if((data[j][0] == data[671][0])):    
        test_data.append(data[j][3])
        date_test.append(str(data[j][10]))
    j = j + 1
    
print(test_data)
print(date_test)

plt.plot(date_train,train_data,'r')
plt.plot(date_test,test_data,'g')
plt.xlabel('dates')
plt.ylabel('openf')
plt.title('Graph of opening Values')
plt.legend()
plt.show()
Posted
Updated 20-Jun-18 5:40am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900