Skip to content

Instantly share code, notes, and snippets.

@mutaku
Last active December 24, 2015 15:29
Show Gist options
  • Save mutaku/6821015 to your computer and use it in GitHub Desktop.
Save mutaku/6821015 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy
import random
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(True)
line1, = plt.plot([], [],'-o', label='MPH', ms=5, lw=2, alpha=0.7, mfc='orange')
line2, = plt.plot([], [], 'c-', label='goal')
plt.ylim((0, 1.1))
x = [0]
goal = 0.8
def update_plot(line, xdata, ydata):
"""Update the line on plot while maintaining goal as constant"""
map(lambda l: l.set_xdata(numpy.append(l.get_xdata(), xdata)), (line, line2))
line.set_ydata(numpy.append(line.get_ydata(), ydata))
line2.set_ydata(numpy.append(line2.get_ydata(), goal))
ax.relim()
ax.autoscale_view(None, True, False)
fig.canvas.draw()
def gen_new_data(n):
"""Generate some new data and update the plot"""
for _ in xrange(n):
new_x = x[-1] + 1
x.append(new_x)
update_plot(line1, new_x, rand_y())
def rand_y():
"""Get data between 0.50 and 1.00 - just for fun"""
return round((random.random() * 100) % 5 / 10 + 0.5, 2)
gen_new_data(100)
import matplotlib.pyplot as plt
import numpy
import random
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(True)
ax.margins(0.1,0.1)
line1, = plt.plot([], [],'-o', label='MPH', ms=5, lw=2, alpha=0.7, mfc='red')
plt.ylim((0, 100))
x = [0,1]
y = [0,1]
def update_plot(line, xdata, ydata):
"""Update the line on plot while maintaining goal as constant"""
line.set_xdata(numpy.append(line.get_xdata(), xdata))
line.set_ydata(numpy.append(line.get_ydata(), ydata))
ax.relim()
ax.autoscale_view(True, True, True)
ax.figure.axes[0].autoscale(enable=True, axis='y')
fig.canvas.draw()
def gen_new_data(n):
"""Generate some new data and update the plot"""
for _ in xrange(n):
new_x = x[-1] + 1
new_y = y[-1] + y[-2]
x.append(new_x)
y.append(new_y)
update_plot(line1, new_x, new_y)
@mutaku
Copy link
Author

mutaku commented Oct 4, 2013

example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment