from matplotlib import pyplot as plt
import collections

def process_line( line, l1, l2):
	words = line.split()
	count = 0
	for word in words:
		if count % 2 == 0:
			l1.append(float(word))    # convert to float number
		else:
			l2.append(float(word))
		count+=1

def process_file( filename,l1,l2):

	fp = open(filename,'r')
	for line in fp:
		line = line.strip()
		process_line(line, l1, l2)


def main():
	l1 = list()
	l2 = list()
	process_file("data.txt",l1,l2)
	print "list 1" + str(l1)
	print "list 2" + str(l2)


	# now draw the line plot
	xs = [i for i,_ in enumerate(l1)]
	plt.plot(xs,l1,"g-",label="list 1")
	plt.plot(xs,l2,"r-",label="list 2")
	plt.legend(loc=9)
	plt.xlabel("example ")
	plt.title("Example of l1 and l2 line plot")
	plt.show()

	# now create bar plot
	xs = [i+0.1 for i,j in enumerate(l1)]
	plt.bar(xs,l2)
	plt.ylabel("values in list")
	plt.title("Test of bar plot")
	plt.xticks([i+0.5 for i,x in enumerate(l1)],l2)
	plt.show()


	# now create decile plot
	decile = lambda grade:(grade//10)*10
	hist  = collections.Counter(decile(grade) for grade in l2)
	plt.bar([x-4 for x in hist.keys()], hist.values(), 8)
	plt.axis([-5,200,0,20])
	plt.xticks([10*i for i in range(20)])
	plt.show()

	# now create a scatter plot
	plt.scatter(l1,l2)
	labels=[]
	for i,_ in enumerate(l1):
		labels.append('N' + str(i))
	#print label
	for label,l11,l22 in zip(labels,l1,l2):
		plt.annotate(label,xy=(l11,l22), xytext=(-5,5), textcoords='offset points')
	plt.show()

if __name__ == "__main__":
	try:
	    main()
	except:
		print("Something is wrong, please make sure you have downloaded data.txt and saved in the current folder")