How to plot all the peaks using Python
I am using a package peakutils
Python
to detect peaks in my data (second column estimated.csv
- found here (click link).
Here is my code to find the peaks:
#/usr/bin/python -tt
import pandas as pd
import peakutils
estimated_data = pd.read_csv("estimated.csv", header=None)
col2 = estimated_data[:][1] # Second column data
print(col2[:]) # Print all the rows
index = peakutils.indexes(col2, thres=0.4, min_dist=1000)
print(index)
Peak detection works fine. I wanted to plot all the detected peaks like in the following tutorial.
https://plot.ly/python/peak-finding/
But does plotly
n't seem to work offline. Is there any other way to do this with packages Python
like matplotlib
?
source to share
Plotting the peaks with matplotlib can be done using a plot with markers. The data is indexed by the index found from the peakutils function.
import pandas as pd
import peakutils
import matplotlib.pyplot as plt
estimated_data = pd.read_csv("data/estimated.csv", header=None)
col1 = estimated_data[:][0] # First column data
col2 = estimated_data[:][1] # Second column data
index = peakutils.indexes(col2, thres=0.4, min_dist=1000)
plt.plot(col1,col2, lw=0.4, alpha=0.4 )
plt.plot(col1[index],col2[index], marker="o", ls="", ms=3 )
plt.show()
To connect the peaks to the line (as pointed out in the comments), on will simply leave ls=""
,
plt.plot(col1[index],col2[index], marker="o", ms=3 )
source to share