Reputation: 572
I took one example from a question and I adapted it to my data set, but when it comes to making plots, I got stuck. I know how to make a date time + values plot, but I didn't figure out how can I make a combination.
The response I took is the following: Response.
The example has the following code:
Example Code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
np.random.seed(42)
# borrowed from @Majid Mortazavi's answer
random_number1 = np.random.randint(0, 200, 20)
random_number2 = np.random.randint(0, 20, 100)
random_number = np.concatenate((random_number1, random_number2))
np.random.shuffle(random_number)
peaks, _ = find_peaks(random_number, height=100)
plt.plot(random_number)
plt.plot(peaks, random_number[peaks], "x")
plt.show()
I adapted it to my real-values dataset and I have the following code:
Adapted Code:
peaks, _ = find_peaks(my_dataset['quality'], height=500)
plt.figure(figsize=(14,12))
x = my_dataset.index
y = my_dataset.quality
plt.scatter(x, y, s=1,c='b',label='quality')
plt.plot(peaks, my_dataset['quality'][peaks], "x")
plt.xlabel('datetime')
plt.ylabel('values')
plt.legend(loc='best')
plt.title('Qualities')
plt.show()
Initially, my code for plotting was this and it worked:
Initial Code:
plt.figure(figsize=(14,12))
x = my_dataset.index
y = my_dataset.quality
plt.scatter(x, y, s=1,c='b',label='quality')
plt.xlabel('datetime')
plt.ylabel('values')
plt.legend(loc='best')
plt.title('Qualities')
plt.show()
But I need to have the same plot, but with the "X" sign on the computed peaks.
So the final result should be something like this: (I edited the initial plot in paint for a visual idea of the final result)
I honestly do not know how to make it work. Can you please help me?
I highly appreciate any effort to help me!!! Thank you so much in advanced!
Upvotes: 2
Views: 802
Reputation: 12496
As documentation of scipy.signal.find_peaks
reports:
Returns: peaks: ndarray
Indices of peaks in x that satisfy all given conditions.
Your peaks
variable is an array of the indices of peaks. So, in order to access x and y coordinates of these peaks, you should replace:
plt.scatter(x, y, s=1,c='b',label='quality')
plt.plot(peaks, my_dataset['quality'][peaks], "x")
with
plt.scatter(x, y, s=1,c='b',label='quality')
plt.plot(x[peaks], y[peaks], "x")
Upvotes: 2