python - Read CSV and plot colored line graph -


i trying plot graph colored markers before , after threshold value. if using loop for reading parsing input file time h:m can plot , color 2 points. points cannot plot.

input

akdj 12:00 34515 sdfg sgqv 13:00 34626 ssfgb dfbb 13:00 14215 gghgws ajdf 13:30 14224 gdgva dsfb 13:45 25672 fw  sfhh 14:00 85597 adsfb 

program

# ma masked array import csv import datetime dt import numpy np import matplotlib.pyplot plt matplotlib.pyplot import  plot  threshold = 30000 x,y = [],[] csv_reader = csv.reader(open('count_time.csv'))  line in csv_reader:     y.append(int(line[2]))     x.append(dt.datetime.strptime(line[1],'%h:%m'))  #plt.figure()  plt.plot(x,y, color='blue')  #add below threshold markers below_threshold = y < threshold plt.scatter(x[below_threshold], y[below_threshold], color='green')   # add above threshold markers above_threshold = np.logical_not(below_threshold) plt.scatter(x[above_threshold], y[above_threshold], color='red')   plt.show() 

wrong output

enter image description here

when used below code read file did not show error showed blank graph layout.

data = np.genfromtxt('count_time.csv', delimiter=",") x = data[:,1] y = data[:,2] 

when changed way following error shown

data = np.loadtxt('count_time.csv', delimiter=',', dtype='str, time, int, str') x = data[:,1] y = data[:,2] 

error

 data = np.loadtxt('count_time.csv', delimiter=',', dtype='str, time, int, str')   file "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 798, in loadtxt     dtype = np.dtype(dtype) typeerror: data type "time" not understood 

you need turn x , y type np.array before calculate above_threshold , below_threshold, , works. in version, don't array of bools, false , true.

i added comma delimiters input csv file make work (i assume should there in first place?)

import csv import datetime dt import numpy np import matplotlib.pyplot plt matplotlib.pyplot import  plot  threshold = 30000 x,y = [],[] csv_reader = csv.reader(open('input.csv'))  line in csv_reader:     y.append(int(line[2]))     x.append(dt.datetime.strptime(line[1],'%h:%m'))  fig=plt.figure()  below_threshold = y < threshold above_threshold = np.logical_not(below_threshold)  print below_threshold # false  print above_threshold # true  x=np.array(x) y=np.array(y)  plt.plot(x,y, color='blue')  #add below threshold markers below_threshold = y < threshold print below_threshold # [false false  true  true  true false]  plt.scatter(x[below_threshold], y[below_threshold], color='green')   # add above threshold markers above_threshold = np.logical_not(below_threshold) print above_threshold # [ true  true false false false  true]  plt.scatter(x[above_threshold], y[above_threshold], color='red')  fig.autofmt_xdate()  plt.show() 

enter image description here


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -