Desta Haileselassie Hagos
Desta Haileselassie Hagos

Reputation: 26086

How to plot all peaks using Python

I am using the peakutils Python package to detect peaks in my data (the second column of estimated.csv - found here (click the 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) 

The peak detection works perfectly fine. I wanted to plot all the detected peaks as it is in the following tutorial.

https://plot.ly/python/peak-finding/

But it seems that plotly doesn't seem to work offline. Is there a different way of doing it using Python packages like matplotlib?

Upvotes: 4

Views: 13017

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339300

Plotting the peaks with matplotlib can be done by 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()

enter image description here

In order to connect the peaks with a line (as asked for in the comments), on would simply leave out the ls="",

plt.plot(col1[index],col2[index], marker="o", ms=3 )

enter image description here

Upvotes: 4

Related Questions