Skip to content

Instantly share code, notes, and snippets.

@bemasher
Created December 7, 2010 04:53
Show Gist options
  • Save bemasher/731473 to your computer and use it in GitHub Desktop.
Save bemasher/731473 to your computer and use it in GitHub Desktop.
import gobject
import matplotlib
matplotlib.use('GTKAgg')
import matplotlib.pyplot as plt
current_pos = 0
temps = []
pad = 5.0
f = plt.figure(figsize=(5.45,4.5), dpi=72)
def update(vars):
# Unpack variables that need to be persistent between
# executions of this method.
temps = vars[0]
current_pos = vars[1]
pad = vars[2]
# Open the data file and get any new data points since
# the last time we read from this file
data = open("out.txt", "r")
data.seek(current_pos)
new_temps = map(lambda x:
float(x) * (1 + 4.0/5.0) + 32.0,
data.read().split("\n")[:-1])
current_pos = data.tell()
data.close()
# If we got new data then append it to the list of
# temperatures and trim to 750 points
if len(new_temps) > 0:
temps.extend(new_temps)
temps = temps[-750:]
f.clear()
f.suptitle("Live Temperature")
a = f.add_subplot(111)
a.grid(True)
l, = a.plot(temps)
plt.xlabel("Time (Seconds)")
plt.ylabel(r'Temperature $^{\circ}$F')
# Get the minimum and maximum temperatures these are
# used for annotations and scaling the plot of data
min_t = min(temps)
max_t = max(temps)
# Add annotations for minimum and maximum temperatures
a.annotate(r'Min: %0.2f$^{\circ}$F' % (min_t),
xy=(temps.index(min_t), min_t),
xycoords='data', xytext=(20, -20),
textcoords='offset points',
bbox=dict(boxstyle="round", fc="0.8"),
arrowprops=dict(arrowstyle="->",
shrinkA=0, shrinkB=1,
connectionstyle="angle,angleA=0,angleB=90,rad=10"))
a.annotate(r'Max: %0.2f$^{\circ}$F' % (max_t),
xy=(temps.index(max_t), max_t),
xycoords='data', xytext=(20, 20),
textcoords='offset points',
bbox=dict(boxstyle="round", fc="0.8"),
arrowprops=dict(arrowstyle="->",
shrinkA=0, shrinkB=1,
connectionstyle="angle,angleA=0,angleB=90,rad=10"))
# Set the axis limits to make the data more readable
a.axis([0,len(temps), min_t - pad,max_t + pad])
f.canvas.draw_idle()
# Repack variables that need to be persistent between
# executions of this method
vars = {0: temps, 1: current_pos, 2: pad}
return True
vars = {0: temps, 1: current_pos, 2: pad}
# Execute update method every 500ms
gobject.timeout_add(500, update, vars)
# Display the plot
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment