Home > Software engineering >  How to extract more points from graph in Python?
How to extract more points from graph in Python?

Time:09-14

I have a graph with 6 cycles for one cycle i given data given bilow and for next cycles same data repeating pasted the graph the star points are actual data points and the black which i manually kept that data has to be extract how to do it in python This is my data:

Efficiency
0
0
59.02
60
56.54
56.54
62.63
61.94
61.94
62.9
59.02
0

And I plotted the graph with this data:

how can we get the data which pointed in blue color

This is the code i used for plotting:

x=df["Efficiency"]
import matplotlib.pyplot as plt
import numpy as np

fig= plt.figure(figsize=(25,6))
axes= fig.add_axes([0.1,0.1,0.8,0.8])
axes.plot(x,color='green',lw=3,marker="*",ms=15,mec = 'r')
plt.xlim(0,72)
plt.ylim(0,65)
plt.show()

[1]: https://i.stack.imgur.com/PqdFT.png

CodePudding user response:

Since your plot is piece-wise linear, I suggest to caculate the corresponding linear curve for each piece:

def interpolate_line(p1, p2, x):
    # p1 and p2 are two of your given points
    # x is a list of positions at which you are interested in the value
    # linear curve y = a*x   b
    
    a = (p2[1]-p1[1])/(p2[0]-p1[0])
    b = (p1[1]*p2[0]-p2[1]*p1[0])/(p2[0]-p1[0])

    # insert all your x values into the linear curve to get their corresponding values
    y = [a*ix   b for ix in x]
    return y

and then feed the points of interest to it:

# generating given points
points = [(ix, iy) for ix, iy in enumerate(x)] # x is your x from the df above
for p1, p2 in zip(points[:-1], points[1:]): 
    corresponding_values = interpolate_line(p1, p2, your_list_of_positions_for_that_piece_of_curve)

CodePudding user response:

for my data "y=df["Efficiency"]"which are 12 values generated x values by using linspace x=np.linspace(0,12,24) generated 24 values and converted array to dataframe and used interpolation method u=np.interp([x], df.x, df.Efficiency) and got the corresponding y values that is stored in "u" finally i tried with my entire data got the graph like thisenter image description here

  • Related