#!/usr/bin/env python # coding: utf-8 # #### New to Plotly? # Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/). #
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online). #
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started! # #### Imports # The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plotly.com/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/) and [PeakUtils](http://pythonhosted.org/PeakUtils/). # In[1]: import plotly.plotly as py import plotly.graph_objs as go import plotly.figure_factory as ff import numpy as np import pandas as pd import scipy import peakutils # #### Import Data # For our baseline detection example, we will import some data on milk production by month: # In[2]: milk_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/monthly-milk-production-pounds.csv') time_series = milk_data['Monthly milk production (pounds per cow)'] time_series = np.asarray(time_series) df = milk_data[0:15] table = ff.create_table(df) py.iplot(table, filename='milk-production-dataframe') # #### Plot with Baseline # In[3]: # calculate baseline y values baseline_values = peakutils.baseline(time_series) trace = go.Scatter( x=[j for j in range(len(time_series))], y=time_series, mode='lines', marker=dict( color='#B292EA', ), name='Original Plot' ) trace2 = go.Scatter( x=[j for j in range(len(time_series))], y=baseline_values, mode='markers', marker=dict( size=3, color='#EB55BF', symbol='circle-open' ), name='Baseline' ) data = [trace, trace2] py.iplot(data, filename='milk-production-plot-with-baseline') # In[4]: from IPython.display import display, HTML display(HTML('')) display(HTML('')) get_ipython().system(' pip install git+https://github.com/plotly/publisher.git --upgrade') import publisher publisher.publish( 'python-Baseline-Detection.ipynb', 'python/baseline-detection/', 'Baseline Detection | plotly', 'Learn how to detect baselines on data in Python.', title='Baseline Detection in Python | plotly', name='Baseline Detection', language='python', page_type='example_index', has_thumbnail='false', display_as='peak-analysis', order=1, ipynb= '~notebook_demo/117') # In[ ]: