Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Generate a graph using
Generate a graph ( from data collected from an Arduino and DS18B20 temperature
sensor. # Learn about API authentication here: and find your api_key here:
''' 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 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)):
return seconds
seconds = get_seconds(data_sensor_B)
''' Step #2: Construct the Graph '''
# Construct the Trace objects
trace0 = Scatter(
trace1 = Scatter(
# 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',
yaxis = yaxis
# Construct the Figure object
fig = Figure(data=data, layout=layout)
''' Step #3: Send the graph to '''
# Send the graph to If using 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