vdu16
vdu16

Reputation: 145

Adding the x-axis values to the peaks in the graph

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.)

Upvotes: 2

Views: 1216

Answers (1)

n1colas.m
n1colas.m

Reputation: 3989

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

Upvotes: 1

Related Questions