2

Dear StackOverflow family,

I have been trying to put the x-axis values next to, or at the top of the peaks that are detected in the graph. Basically, first I used the function to find the peaks in the spectrum. Then, I want to use these x-axis values that coincide with the peaks, not just only putting "x", or any kind of symbol.

Thank you for any kind of help or suggestion.

The codes;

    peaks, properties = find_peaks(meanMat1, prominence=1, width=4)
    peak_coordinates = list(zip(Ram[peaks], a[peaks])) 
    
    print(peak_coordinates)
   
    d=Ram[peaks]
    e=c[peaks]
    
    ax.plot(d, e, "x", color = "xkcd:orange")

(Here, d and e are the peaks that are detected. d and e give x-axis and y-axis values (in np.array), respectively.)

2
  • are you using matplotlib.pyplot? Commented Apr 22, 2022 at 12:46
  • @NicoCaldo yes, for plotting. to find peaks, i use from scipy.signal import find_peaks Commented Apr 22, 2022 at 12:47

1 Answer 1

1

You can use Matplotlib text to add a string, or, in your particular case the x-axis values, to the Axes at location x (d in your example), y (e in your example) in data coordinates.

import matplotlib.pyplot as plt
from scipy.signal import find_peaks
import numpy as np

# curve setup
x = np.linspace(0,10*np.pi,200)
fx = np.sin(2*x)*np.cos(x/2)*np.exp(-0.1*x)

peaks, _ = find_peaks(fx, prominence=0.4)
peak_coordinates = list(zip(peaks, fx[peaks]))

fig, ax = plt.subplots()
ax.plot(fx)
ax.plot(peaks, fx[peaks], "x")

y0,y1=ax.get_ylim()
ax.set_ylim(y0,y1*1.1) # fix top y-limit

for xp,yp in peak_coordinates:
    plt.text(xp*1.05, yp*1.05, xp, fontsize=10)

plt.show()

peak_values

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.