Skip to content

Commit 667743f

Browse files
committed
peak-finding tutorial
1 parent c86efa2 commit 667743f

File tree

1 file changed

+55
-76
lines changed

1 file changed

+55
-76
lines changed

unconverted/python/peak-finding.md

+55-76
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ jupyter:
88
format_version: '1.1'
99
jupytext_version: 1.1.1
1010
kernelspec:
11-
display_name: Python 2
11+
display_name: Python 3
1212
language: python
13-
name: python2
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.7
1424
plotly:
1525
description: Learn how to find peaks and valleys on datasets in Python
1626
display_as: peak-analysis
@@ -26,131 +36,100 @@ jupyter:
2636
title: Peak Finding in Python | plotly
2737
---
2838

29-
#### New to Plotly?
30-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
31-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
32-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
33-
34-
3539
#### Imports
36-
The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plot.ly/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/) and [PeakUtils](http://pythonhosted.org/PeakUtils/).
40+
The tutorial below imports [Pandas](https://plot.ly/pandas/intro-to-pandas-tutorial/), and [SciPy](https://www.scipy.org/).
3741

3842
```python
39-
import plotly.plotly as py
40-
import plotly.graph_objs as go
41-
from plotly.tools import FigureFactory as FF
42-
43-
import numpy as np
4443
import pandas as pd
45-
import scipy
46-
import peakutils
44+
from scipy.signal import find_peaks
4745
```
4846

4947
#### Import Data
5048
To start detecting peaks, we will import some data on milk production by month:
5149

5250
```python
51+
import plotly.graph_objects as go
52+
import pandas as pd
53+
5354
milk_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/monthly-milk-production-pounds.csv')
5455
time_series = milk_data['Monthly milk production (pounds per cow)']
55-
time_series = time_series.tolist()
5656

57-
df = milk_data[0:15]
58-
59-
table = FF.create_table(df)
60-
py.iplot(table, filename='milk-production-dataframe')
61-
```
62-
63-
#### Original Plot
64-
65-
```python
66-
trace = go.Scatter(
67-
x = [j for j in range(len(time_series))],
57+
fig = go.Figure(data=go.Scatter(
6858
y = time_series,
6959
mode = 'lines'
70-
)
60+
))
7161

72-
data = [trace]
73-
py.iplot(data, filename='milk-production-plot')
62+
fig.show()
7463
```
7564

76-
#### With Peak Detection
65+
#### Peak Detection
66+
7767
We need to find the x-axis indices for the peaks in order to determine where the peaks are located.
7868

7969
```python
80-
cb = np.array(time_series)
81-
indices = peakutils.indexes(cb, thres=0.02/max(cb), min_dist=0.1)
70+
import plotly.graph_objects as go
71+
import pandas as pd
72+
from scipy.signal import find_peaks
73+
74+
milk_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/monthly-milk-production-pounds.csv')
75+
time_series = milk_data['Monthly milk production (pounds per cow)']
76+
77+
indices = find_peaks(time_series)[0]
8278

83-
trace = go.Scatter(
84-
x=[j for j in range(len(time_series))],
79+
fig = go.Figure()
80+
fig.add_trace(go.Scatter(
8581
y=time_series,
86-
mode='lines',
82+
mode='lines+markers',
8783
name='Original Plot'
88-
)
84+
))
8985

90-
trace2 = go.Scatter(
86+
fig.add_trace(go.Scatter(
9187
x=indices,
9288
y=[time_series[j] for j in indices],
9389
mode='markers',
9490
marker=dict(
9591
size=8,
96-
color='rgb(255,0,0)',
92+
color='red',
9793
symbol='cross'
9894
),
9995
name='Detected Peaks'
100-
)
96+
))
10197

102-
data = [trace, trace2]
103-
py.iplot(data, filename='milk-production-plot-with-peaks')
98+
fig.show()
10499
```
105100

106101
#### Only Highest Peaks
107102
We can attempt to set our threshold so that we identify as many of the _highest peaks_ that we can.
108103

109104
```python
110-
cb = np.array(time_series)
111-
indices = peakutils.indexes(cb, thres=0.678, min_dist=0.1)
105+
import plotly.graph_objects as go
106+
import numpy as np
107+
import pandas as pd
108+
from scipy.signal import find_peaks
109+
110+
milk_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/monthly-milk-production-pounds.csv')
111+
time_series = milk_data['Monthly milk production (pounds per cow)']
112+
113+
indices = find_peaks(time_series, threshold=20)[0]
112114

113-
trace = go.Scatter(
114-
x=[j for j in range(len(time_series))],
115+
fig = go.Figure()
116+
fig.add_trace(go.Scatter(
115117
y=time_series,
116-
mode='lines',
118+
mode='lines+markers',
117119
name='Original Plot'
118-
)
120+
))
119121

120-
trace2 = go.Scatter(
122+
fig.add_trace(go.Scatter(
121123
x=indices,
122124
y=[time_series[j] for j in indices],
123125
mode='markers',
124126
marker=dict(
125127
size=8,
126-
color='rgb(255,0,0)',
128+
color='red',
127129
symbol='cross'
128130
),
129131
name='Detected Peaks'
130-
)
131-
132-
data = [trace, trace2]
133-
py.iplot(data, filename='milk-production-plot-with-higher-peaks')
134-
```
135-
136-
```python
137-
from IPython.display import display, HTML
138-
139-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
140-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
141-
142-
! pip install git+https://github.com/plotly/publisher.git --upgrade
143-
import publisher
144-
publisher.publish(
145-
'python-Peak-Finding.ipynb', 'python/peak-finding/', 'Peak Finding | plotly',
146-
'Learn how to find peaks and valleys on datasets in Python',
147-
title='Peak Finding in Python | plotly',
148-
name='Peak Finding',
149-
language='python',
150-
page_type='example_index', has_thumbnail='false', display_as='peak-analysis', order=3,
151-
ipynb= '~notebook_demo/120')
152-
```
153-
154-
```python
132+
))
155133

134+
fig.show()
156135
```

0 commit comments

Comments
 (0)