3

I am trying to plot a graph with colored markers before and after threshold value. If I am using for loop for reading the parsing the input file with time H:M I can plot and color only two points. But for all the points I 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 as dt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import  plot

threshold = 30000
x,y = [],[]
csv_reader = csv.reader(open('Count_Time.csv'))

for 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 I used the below code to read file that did not show any error but showed a blank graph layout.

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

When changed this way following error is 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

1 Answers1

0

you need to turn x and y into type np.array before you calculate above_threshold and below_threshold, and then it works. In your version, you don't get an array of bools, but just False and True.

I added comma delimiters to your input csv file to make it work (I assume the should be there in the first place?)

import csv
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import  plot

threshold = 30000
x,y = [],[]
csv_reader = csv.reader(open('input.csv'))

for 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

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Great .. Thanks this works awesome for me.. I need to put `plt.autofmt_xdate()` that give me some error. –  Jun 15 '15 at 12:17
  • 1
    `autofmt_xdate` is a method of `plt.figure`, so you need to have `fig=plt.figure` and then `fig.autofmt_xdate()` (see my edit above) – tmdavison Jun 15 '15 at 12:40
  • Thank you so much .. one last query how can I have legends for colored markers. `plt.legend` doesn't work –  Jun 15 '15 at 13:49
  • 1
    you just need to give your `plt.scatter` plot a `label`. e.g. `plt.scatter(x[below_threshold], y[below_threshold], color='green',label='below')` before you call `plt.legend()` – tmdavison Jun 15 '15 at 14:13