Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a question \ problem. I need to plot the graph by the numbers that I got from the file (which I did) and then I need to draw a line connecting start and end, and calculate the area that between these two lines. I try to make a lot of variations, but i have no idea how I can make it..

I'm trying do it via matplotlib.pyplot library

enter image description here

Here the 'figure' whitch I should to get after add 'connection line between beginning and and' and now I need calcutale square between black line and blue. PS the black one is kind of straight :)

enter image description here

Here is soure of code, and my data file... http://pastebin.com/g40bAzPR

#!/path/to/python -tt

# numerical data
# python GraphicalPart.py ../dataFile.txt

import sys
import matplotlib.pyplot as plt
import numpy as np

def startDivide(fileName):
 for i in range(1,2):
    inputFile = open(fileName)
    outputFile = open(fileName + "_" + str(i) + "_out.csv", "w")
    floatList = []
    for line in inputFile.readlines():
        data = line.split(" ")
        string = data[i]
        if string.startswith('-'): #remove '-'
            string = string[1:]
        floatList.append(float(string))

    floatList.sort() #sorting the list of data

    for item in floatList:
        outputFile.write("%s\n" % item)

    outputFile.close() 
    inputFile.close()

    data1=np.genfromtxt(fileName + "_" + str(i) + '_out.csv', skip_header=1)
    plt.plot(data1) 
    plt.savefig(fileName + "_" + str(i) + "_.png")
    plt.clf()

def main():
 if len(sys.argv) != 2:
  print "Not enough arguments. *_data.txt file only!"
 else:
  startDivide(sys.argv[1])

if __name__ == "__main__":
 main()
share|improve this question
    
I mean, calculate square of 'figure' which is obtained after the line between the beginning and the end –  HelloWorld yesterday
    
Added more details to post –  HelloWorld yesterday

1 Answer 1

up vote 2 down vote accepted

for i in range(1,2) is a loop which only iterates once. Maybe you plan on increasing the number of iterations? If so, bear in mind that it's quicker to load the data once, rather than multiple times in a for-loop. You can do that using np.genfromtxt with the usecols parameter to specify the desired columns.

To find the area under the curve, you could use np.trapz. To find the area between two curves, you subtract area under the upper curve from the area under the lower curve. Assuming the diagonal line is always above the data curve:

import sys
import matplotlib.pyplot as plt
import numpy as np

def startDivide(filename):
    data = np.genfromtxt(filename, dtype=None, usecols=[1])
    data = np.abs(data)
    data.sort()
    np.savetxt("{}_1_out.csv".format(filename), data)
    plt.plot(data)
    plt.plot([0,len(data)-1], [data[0], data[-1]])
    plt.savefig("{}_1_.png".format(filename))
    area = np.trapz([data[0], data[-1]], dx=len(data)-1) - np.trapz(data)
    print(area)

if __name__ == "__main__":
    startDivide(sys.argv[1])
share|improve this answer
    
Thanks for your answer, about for i in range(1,2), This is for test, in future here I will use for whole colums (1,8).. And yes, your code looks better than mine :) but I got an error for 'filename' like "NameError: global name 'filename' is not define..." :\ –  HelloWorld yesterday
    
I tried you plot line and counting area on my code - it's work good! thanks a lot. Could you please vote for my topic, I need more score :) thanks –  HelloWorld yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.