Search code examples
pythonpercentagedifference

How to find the percentage change of two graphs


I have two graphs and would like to find the difference between them in percentage.

First I converted them into lists:

Graph_1=[3843.788185, 3734.256494, 3572.574207, 3420.958554, 3254.79344, 3008.104688, 2804.140772, 2630.084873, 2455.295009, 2324.022285, 2159.982356, 2052.704012, 1997.796418, 1919.717952, 1891.275495, 1887.750553, 1882.098642, 1907.227429, 1900.963964, 1869.716515]

Graph_2= [5825.787588, 5712.159078, 5463.242265, 5310.681583, 5174.471092, 4978.994489, 5134.328055, 5040.945936, 4591.175423, 4001.873071, 4030.536351, 4178.250614, 4132.22026, 4028.463262, 4000.654956, 4123.892956, 4106.260715, 4118.031442, 4116.180353, 4124.077156]

I do not need to know if they have common values, only how close they are together.

I tried this:

res = len(set(Graph_1) and set(Graph_2)) / float(len(set(Graph_1) or set(Graph_2))) * 100

print("Percentage similarity among graphs is : " + str(res))


Solution

  • First of all, you should check whenever the two graphs have the same amount of data (length of the lists)

    Then I think what you want to achieve is something like that:

    percentage_differences = []
    for val1, val2 in zip(Graph_1, Graph_2):
        difference = abs(val1 - val2)
        percentage_difference = (difference / val1) * 100
        percentage_differences.append(percentage_difference)
    
    average_percentage_difference = sum(percentage_differences) / len(percentage_differences)
    

    You can then print the value as you like.