简体   繁体   English

通过matplotlib.pyplot在Python中绘图(计算面积)

[英]Plotting in Python via matplotlib.pyplot (calculate the area)

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 我正在尝试通过matplotlib.pyplot库执行此操作

在此处输入图片说明

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 :) PS黑色一个是直的:)

在此处输入图片说明

Here is soure of code, and my data file... http://pastebin.com/g40bAzPR 这是源代码,还有我的数据文件... 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()

for i in range(1,2) is a loop which only iterates once. for i in range(1,2)中的for i in range(1,2)是仅循环一次的循环。 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. 如果是这样,请记住, 一次加载数据更快,而不是在for循环中多次加载。 You can do that using np.genfromtxt with the usecols parameter to specify the desired columns. 你可以做,使用np.genfromtxtusecols参数指定所需的列。

To find the area under the curve, you could use np.trapz . 要找到曲线下的面积,可以使用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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM