0

I have a txt file that contains x and y points. I plot the graph using matplotlib.

My graph plot looks something like Graph

    ....
    graph=path+'/profile.txt'
    x, y = loadtxt(graph, unpack=True)
    ax.plot(x,y)
    ....

As it can be noticed that from 2 in the x-direction, there are small bubbles. I want to count the number of bubbles and measure the maximum height (in the y-direction) for each bubble.

The gap between the bubbles is a straight line along the x-axis.

My line is not continuous as seen below enter image description here

My profile text file - https://www.dropbox.com/s/oc8vjctc5vt2xfp/profile.txt?dl=0

[profile.txt][3]

Any guidance is appreciated.

MMM
  • 11
  • 3
  • And the code that generates this graphical output is what? – Mr. T Mar 08 '21 at 17:34
  • Bubbles mean oscillations of the blue line right? – Mr. Hobo Mar 08 '21 at 17:35
  • 1
    There are many similar questions [find peaks location in a spectrum](https://stackoverflow.com/questions/24656367/find-peaks-location-in-a-spectrum-numpy), [Finding local maxima/minima with Numpy in a 1D numpy array](https://stackoverflow.com/questions/4624970/finding-local-maxima-minima-with-numpy-in-a-1d-numpy-array), .... `scipy.signal.find_peaks` seems a suitable function. Some tweaking will be needed for the parameters. – JohanC Mar 08 '21 at 17:38
  • Thank you for the prompt responses. @JohanC I tried the links you provided but since my graph is not continuous, I am still unable to get the maxima using `peaks, _ = find_peaks(x, height=0)` logic – MMM Mar 09 '21 at 11:02
  • Well, without reproducible data it is hard to start experimenting to find good parameters. Are the missing values `NaN` in the y array? Or is there just a larger distance between the x's? Maybe you could add the values for x and y for x between 2 and 2.4 or so? – JohanC Mar 09 '21 at 11:36
  • @JohanC I have uploaded the profile.txt file for a better understanding of my problem. The outline of the profile shows the contour and when the bubble doesn't exist, the x point jumps for instance from 0.0028196m (2.819mm) to 0.0029252m (2.9252mm) (Row1077-1078). – MMM Mar 09 '21 at 12:00
  • 1
    But the suggested `peaks, _ = find_peaks(df.Y, height=0.000005)` finds all peaks (considering that your data are read from file into a pandas dataframe `df` with column names `X` and `Y`). What is your additional problem? – Mr. T Mar 09 '21 at 12:05
  • @Mr.T I am not using dataframe. I have tried `peaks, _ = find_peaks(x, height=0) ax.plot(peaks,y[peaks],"x")` but nothing plots. When I try printing `y[peaks]`, the array returns empty – MMM Mar 09 '21 at 12:13
  • You load `x, y` as numpy arrays, so there should be no problem. In this case, you determine the peaks with `peaks, _ = find_peaks(y, height=0.000005)` and plot the peaks with `plt.plot(x[peaks], y[peaks], "s")`. Nothing different from what the duplicate suggests. – Mr. T Mar 09 '21 at 12:20
  • @Mr.T This is brilliant, worked like a charm! Can you give me some guidance with counting the number of bubbles? I was thinking of counting the maxima but since I am getting peaks before bubbles are formed, is there a way I can say start counting peaks from this x position onwards? Like `plt.plot(np.zeros_like(x), "--", color="gray")` in https://stackoverflow.com/questions/4624970/finding-local-maxima-minima-with-numpy-in-a-1d-numpy-array was used to only consider beyond 0.0. Anything like that? – MMM Mar 09 '21 at 12:27
  • @Mr.T yes, I was using `len(y[peaks])`, got the same result but I want to start counting from some distance onwards, say 2 onwards? How can I make that possible? I tried doing `peaks.size>2`, I get True but how do I list all the peaks after 2? – MMM Mar 09 '21 at 12:52
  • Peaks is an index array, so `peaks[peaks>600].size` (all indexes above 600) or `peaks[peaks>np.where(y==0)[0][0]].size` (all indexes after the first y-value being zero) should work. – Mr. T Mar 09 '21 at 13:02
  • great help! Many thanks. – MMM Mar 10 '21 at 07:12

0 Answers0