Last active
August 29, 2015 14:17
-
-
Save kbarre123/9f2696d12ac868bcebab to your computer and use it in GitHub Desktop.
Generate a graph using Plot.ly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Generate a Plot.ly graph (http://plot.ly) from data collected from an Arduino and DS18B20 temperature | |
sensor. # Learn about API authentication here: https://plot.ly/python/getting-started and find your api_key here: https://plot.ly/settings/api | |
''' | |
''' Step #1: Prepare the Data ''' | |
# (*) To communicate with Plotly's server, sign in with credentials file | |
import plotly.plotly as py | |
# (*) Useful Python/Plotly tools | |
import plotly.tools as tls | |
# (*) Graph objects to piece together plots | |
from plotly.graph_objs import * | |
import numpy as np # (*) numpy for math functions and arrays | |
# Define a file reader function | |
def read_data(file_name): | |
''' | |
Read row of data file, return a numpy array | |
pos. arg (1) filepath: relative path to data file | |
''' | |
with open(file_name, 'r') as f: | |
#return np.array([float(x) for x in row]) | |
return np.array([float(x.strip('\n')) for x in f.readlines()]) | |
# Read data in (will be plotted along the Y axis) | |
data_sensor_A = read_data('data/amb_sensor_A.txt') | |
data_sensor_B = read_data('data/amb_sensor_B.txt') | |
# Define a function to calculate the length of time data was collected. This will be the X axis. | |
def get_seconds(data_array): | |
seconds = [] | |
for i in range(0, len(data_array)): | |
seconds.append(i) | |
return seconds | |
seconds = get_seconds(data_sensor_B) | |
''' Step #2: Construct the Graph ''' | |
# Construct the Trace objects | |
trace0 = Scatter( | |
x=seconds, | |
y=data_sensor_A, | |
mode='lines' | |
) | |
trace1 = Scatter( | |
x=seconds, | |
y=data_sensor_B, | |
mode='lines' | |
) | |
# Construct the Data object | |
data = Data([trace0, trace1]) | |
# Construct the Layout object | |
xaxis = XAxis(title='Seconds') | |
yaxis = YAxis(title='Temperature (*F)') | |
layout = Layout( | |
title='DS18B20 Temp Sensor Data', | |
xaxis1=xaxis, | |
yaxis = yaxis | |
) | |
# Construct the Figure object | |
fig = Figure(data=data, layout=layout) | |
''' Step #3: Send the graph to Plot.ly ''' | |
# Send the graph to Plot.ly. If using Plot.ly from within an IPython notebook, replace | |
# the plot() function with iplot() to also embed the graph into the notebook. | |
py.plot(fig, filename='DS18B20 Temp Sensor Data') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment