I have code that produces a live graph, updating every few seconds. It all functions EXACTLY as I want other than a single issue, the x axis keeps adding new values but never removing old ones
in the example code below, because I limit the dataframe to 6 columns, I expect to never see more than 6 measurements represented on my x-axis. Instead, the graph continues to update and eventually the poiunts are too close together.
from matplotlib import pyplot
from matplotlib.animation import FuncAnimation
import pandas as pd
from datetime import datetime
import threading
import random
import time
measurements = ['abc','bcd','afr','reg','wow']
counter = 0
figure = pyplot.figure()
measurement_frame = pd.DataFrame(index = measurements)
def get_live(counter2, col_num):
measurement_frame.iat[counter2,col_num] = random.randint(50,80)
def add_to_dataframe():
global measurement_frame
#timey = datetime.now().strftime('%H:%M:%S')
timey = datetime.now().time()
if measurement_frame.shape[1] == 6:
measurement_frame.drop(measurement_frame.columns[0], axis = 1, inplace = True)
measurement_frame[timey] = measurements
col_num = measurement_frame.shape[1]-1
print(col_num)
counter2 = 0
for item in measurements:
t = threading.Thread(target=get_live, args=(counter2, col_num,))
t.start()
counter2 = counter2 +1
t.join()
print(measurement_frame.columns[0])
time.sleep(1)
def update(frame):
add_to_dataframe()
x_data = measurement_frame.columns
print(x_data[0])
y1_data = measurement_frame.loc[measurement_frame.index[0]]
y2_data = measurement_frame.loc[measurement_frame.index[1]]
y3_data = measurement_frame.loc[measurement_frame.index[2]]
y4_data = measurement_frame.loc[measurement_frame.index[3]]
y5_data = measurement_frame.loc[measurement_frame.index[4]]
line, = pyplot.plot_date(x_data, y1_data, '-', color = 'b')
line2, = pyplot.plot_date(x_data, y2_data, '-', color = 'g')
line3, = pyplot.plot_date(x_data, y3_data, '-', color = 'r')
line4, = pyplot.plot_date(x_data, y4_data, '-', color = 'm')
line5, = pyplot.plot_date(x_data, y5_data, '-', color = 'y')
line.set_data(x_data, y1_data)
line2.set_data(x_data, y2_data)
line3.set_data(x_data, y3_data)
line4.set_data(x_data, y4_data)
line5.set_data(x_data, y5_data)
figure.gca().set_xlim(x_data[0])
figure.gca().autoscale()
print(figure.gca().get_xlim())
return line, line2, line3, line4, line5,
animation = FuncAnimation(figure, update, interval=1000)
pyplot.show()
What I need is that after the dataframe reaches maximum size, the far left measurements are removed, so as to not exceed a set number of measurements on the screen at once. Note that the dataframe already drops unneeded columns before adding a new one when it reaches a certain size, but my graph does not reflect that
using autoscale tries to keep old data in view. If you drop autoscale and use
figure.gca().set_xlim(left =x_data[0], right = datetime.now().time())
it works as intended
the full code is now
from matplotlib import pyplot
from matplotlib.animation import FuncAnimation
import pandas as pd
from datetime import datetime
import threading
import random
import time
measurements = ['abc','bcd','afr','reg','wow']
counter = 0
figure = pyplot.figure()
measurement_frame = pd.DataFrame(index = measurements)
def get_live(counter2, col_num):
measurement_frame.iat[counter2,col_num] = random.randint(50,80)
def add_to_dataframe():
global measurement_frame
#timey = datetime.now().strftime('%H:%M:%S')
timey = datetime.now().time()
if measurement_frame.shape[1] == 6:
measurement_frame.drop(measurement_frame.columns[0], axis = 1, inplace = True)
measurement_frame[timey] = measurements
col_num = measurement_frame.shape[1]-1
print(col_num)
counter2 = 0
for item in measurements:
t = threading.Thread(target=get_live, args=(counter2, col_num,))
t.start()
counter2 = counter2 +1
t.join()
print(measurement_frame.columns[0])
time.sleep(1)
def update(frame):
add_to_dataframe()
x_data = measurement_frame.columns
print(x_data[0])
y1_data = measurement_frame.loc[measurement_frame.index[0]]
y2_data = measurement_frame.loc[measurement_frame.index[1]]
y3_data = measurement_frame.loc[measurement_frame.index[2]]
y4_data = measurement_frame.loc[measurement_frame.index[3]]
y5_data = measurement_frame.loc[measurement_frame.index[4]]
line, = pyplot.plot_date(x_data, y1_data, '-', color = 'b')
line2, = pyplot.plot_date(x_data, y2_data, '-', color = 'g')
line3, = pyplot.plot_date(x_data, y3_data, '-', color = 'r')
line4, = pyplot.plot_date(x_data, y4_data, '-', color = 'm')
line5, = pyplot.plot_date(x_data, y5_data, '-', color = 'y')
line.set_data(x_data, y1_data)
line2.set_data(x_data, y2_data)
line3.set_data(x_data, y3_data)
line4.set_data(x_data, y4_data)
line5.set_data(x_data, y5_data)
figure.gca().set_xlim(left =x_data[0], right = datetime.now().time())
print(figure.gca().get_xlim())
return line, line2, line3, line4, line5,
animation = FuncAnimation(figure, update, interval=1000)
pyplot.show()
Related
I'm very early in making a simple graph to plot the temperature and humidity of a given space however, the graph plots both lines but they don't intersect instead they do something like in the image below:
here's the code:
import matplotlib.pyplot as plt
from itertools import count
from matplotlib.animation import FuncAnimation
import serial
import matplotlib as mpl
import numpy as np
x = []
line1 = []
line2 = []
serialPort = serial.Serial(port = "COM3", baudrate=115200,
bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE)
serialString = ""
index = count()
def animate(i):
if serialPort.in_waiting > 0:
serialString = serialPort.readline()
string = serialString.decode('Ascii')
line1.append(string.split(' ')[0])
line2.append(string.split(' ')[1])
x.append(next(index))
print(len(line1))
if len(line1) >= 1000:
line1.pop(0)
line2.pop(0)
x.pop(0)
plt.cla()
plt.plot(x, line1, label='Temperature')
plt.plot(x, line2, label='Humidity')
ani = FuncAnimation(plt.gcf(), animate, interval=400)
plt.tight_layout()
plt.show()
I try to create a live graph with matplotlib animation. below code report error:
UnboundLocalError: local variable 'lines' referenced before assignment
lines is defined above!
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
queue = []
qSize = 20
N = 3
lines = []
xidx = list(range(0, qSize))
def init():
for i in range(qSize):
queue.append([0]*N)
for i in range(N):
xdata = []
ydata = []
line, = ax.plot(xdata, ydata,label=str(i))
lines.append([line,xdata,ydata])
ax.legend(loc=2)
plt.xticks(rotation=45, ha='right')
#plt.subplots_adjust(bottom=0.30)
plt.title('Demo')
plt.ylabel('x')
plt.ylabel('Value')
return
def tick():
arr = []
for i in range(N):
d = random.randint(i*10,i*10+5)
arr.append(d)
queue.append(arr)
lines = lines[-qSize:]
return
def animate(i):
tick()
df = pd.DataFrame(data)
ax.set_xlim(0,qSize)
ax.set_ylim(df.min().min(),df.max().max())
for i,colname in enumerate(df.columns):
line,xdata,ydata = lines[i]
xdata = xidx
ydata = df[colname]
line.set_xdata(xdata)
line.set_ydata(ydata)
plt.draw()
return
init()
ani = animation.FuncAnimation(fig, animate, fargs=(), interval=1000)
plt.show()
I'm using a Raspberry Pi to plot live data from serial, but eventually run out of memory. I'm not sure if/how I can close the figure, but still have a live data display.
Would it be possible to create and close a new figure with every animate?
My code at the moment:
import serial
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('TkAgg') #comment out for debugging
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import gc
# Create figure for plotting
fig = plt.figure()
xs = []
ysAC = []
ysDC = []
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
ser.flush()
# This function is called periodically from FuncAnimation
def animate(i, xs, ysAC, ysDC):
values = getValues()
wAC = values[1]
wDC = values[2]
# Add x and y to lists
xs.append(i)
ysAC.append(wAC)
ysDC.append(wDC)
# Limit x and y lists to 10 items
xs = ['T-9','T-8','T-7','T-6','T-5','T-4','T-3','T-2','T-1','Now']
ysDC = ysDC[-10:]
ysAC = ysAC[-10:]
# Draw x and y lists
axRT1.clear()
if len(ysDC) == 10:
lineAC, = axRT1.plot(xs, ysAC, 'b:', label='Mains', linewidth = 4)
lineDC, = axRT1.plot(xs, ysDC, 'g--', label='Solar', linewidth = 4)
gc.collect()
#fig.clf()
#plt.close()
def getValues():
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
return list(line.split(","))
# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ysAC, ysDC), interval=1000, blit=False)
plt.get_current_fig_manager().full_screen_toggle()
plt.ioff()
plt.show()
plt.draw()
The crude way of clearing the plots marked below fixed it for me:
import time
import serial
import datetime as dt
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('TkAgg') #comment out for debugging
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.animation as animation
from decimal import Decimal
import pandas as pd
import numpy as np
import os.path as path
import re
import gc
import os
count = 0
# Create figure for plotting
fig = plt.figure()
fig.patch.set_facecolor('whitesmoke')
hFont = {'fontname':'sans-serif', 'weight':'bold', 'size':'12'}
xs = ['T-9','T-8','T-7','T-6','T-5','T-4','T-3','T-2','T-1','Now']
ysTemp = []
ysAC = []
ysDC = []
axRT1 = fig.add_subplot(2, 2, 1)
axRT2 = axRT1.twinx() # instantiate a second axes that shares the same x-axis
#Draw x and y lists
axRT1.clear()
axRT2.clear()
axRT1.set_ylim([0, 4])
axRT2.set_ylim([10, 70])
axRT1.set_ylabel('Power Consumption kW', **hFont)
axRT2.set_ylabel('Temperature C', **hFont)
axRT1.set_xlabel('Seconds', **hFont)
axRT1.set_title('Power Consumption and Temperature - Real Time', **hFont)
lineTemp, = axRT2.plot([], [], 'r', label='Temp', linewidth = 4)
lineAC, = axRT1.plot([], [], 'b:', label='Mains', linewidth = 4)
lineDC, = axRT1.plot([], [], 'g--', label='Solar', linewidth = 4)
fig.legend([lineAC, lineDC,lineTemp], ['Mains', 'Solar', 'Temp'], fontsize=20)
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
ser.flush()
# This function is called periodically from FuncAnimation
def animate(i, xs, ysTemp, ysAC, ysDC):
values = getValues()
if values != 0:
temp_c = Decimal(re.search(r'\d+',values[3]).group())
if temp_c < 0: temp_c = 0
wAC = round(Decimal(re.search(r'\d+', values[1]).group())/1000, 2)
if wAC < 0.35: wAC = 0
aDC = float(re.search(r'\d+', values[2]).group()) #remove characters
vDC = float(re.search(r'\d+', values[4][:5]).group()) #remove characters
wDC = aDC * vDC
wDC = round(abs(Decimal(wDC))/1000, 2)
# Add x and y to lists
ysTemp.append(temp_c)
ysAC.append(wAC)
ysDC.append(wDC)
# Limit x and y lists to 10 items
ysTemp = ysTemp[-10:]
ysDC = ysDC[-10:]
ysAC = ysAC[-10:]
if len(ysTemp) == 10:
axRT2.lines = [] #This crude way of clearing the plots worked
axRT1.lines =[] #This crude way of clearing the plots worked
lineTemp, = axRT2.plot(xs, ysTemp, 'r', label='Temp', linewidth = 4)
lineAC, = axRT1.plot(xs, ysAC, 'b:', label='Mains', linewidth = 4)
lineDC, = axRT1.plot(xs, ysDC, 'g--', label='Solar', linewidth = 4)
def getValues():
measureList = 0
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
if line.count(',') == 4:
measureList = list(line.split(","))
return measureList
# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ysTemp, ysAC, ysDC), interval=1000, blit=False)
plt.get_current_fig_manager().full_screen_toggle()
plt.ioff()
plt.show()
plt.draw()
I'm sorry if my title is very unclear. I am working on an animation that represents a histogram of all the numbers as it crawls through a file.
I'm trying to get a unique ID, but they're all returning the same id. Then I thought about setting a unique ID, but I'm not entirely sure how to do that. I was thinking if I could just isolate the result in the sequence to be its own bar, I could clear it and then update it.
It's really hard to imagine what's going on so here's an image:
Here is the code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import numpy as np
import random
from collections import defaultdict
from collections import deque
import re
color = f"#{random.randrange(0x1000000):06x}"
color_border = f"#{random.randrange(0x1000000):06x}"
range = [0,1,2,3,4,5,6,7,8,9]
digit_finder = re.compile(r'(\d+)')
fig = plt.figure()
ax = fig.add_subplot(111, projection= '3d')
ax.set_xticks(range)
#def text_crawler(file, place, bptrial):
def animate(i):
global z_line
global color
global color_border
_last = _f.readlines(20)
for line in _last:
_line = line.decode("utf-8")
final = digit_finder.findall(_line)
#if not final: continue
print(final[1])
z_line.append(int(final[1]))
for z in z_line:
squared_num = createdict(str(z))
xs = list(squared_num.keys())
ys = list(squared_num.values())
#ax.clear()
ax.bar(xs, ys, zs=z ,zdir='y', color = color, alpha=0.8,
linewidth=3, edgecolor = color_border)
ax.set_yticks(z_line)
ax.set_xticks(range)
print(Axes3D.bar)
def createdict(it):
x = defaultdict(int)
for k in it:
x[k] += 1
return x
count = 10
#final = list(map(lambda x: createdict(x), nums))
with open('graph_index.txt', 'rb') as index_reader:
i_first = index_reader.readline()
if not i_first:
print('This is where you return index = 0')
index_reader.seek(-2, 2)
while index_reader.read(1) != b"\n":
index_reader.seek(-2,1)
i_last = index_reader.readline().decode("utf-8")
index = digit_finder.findall(i_last)
if not index:
print('This is also index = 0...just in case.')
else:
print(int(index[0]), 'This should be a number, in bytes, where you will continue from.')
z_line = deque([],maxlen=7)
with open('numbers.txt', 'rb') as _f:
first = _f.readline(0)
_f.seek(int(index[0]), 0)
while _f.read(1) != b"\n":
_f.seek(-2,1)
ani = animation.FuncAnimation(fig, animate, interval=10000)
print(ani)
plt.show()```
Can anybody help how to optimize the plot function in python? I use Matplotlib to plot financial data.Here small function for plotting OHLC data. The time increase significantly if I add indicators or other data.
import numpy as np
import datetime
from matplotlib.collections import LineCollection
from pylab import *
import urllib2
def test_plot(OHLCV):
bar_width = 1.3
date_offset = 0.5
fig = figure(figsize=(50, 20), facecolor='w')
ax = fig.add_subplot(1, 1, 1)
labels = ax.get_xmajorticklabels()
setp(labels, rotation=0)
month = MonthLocator()
day = DayLocator()
timeFmt = DateFormatter('%Y-%m-%d')
colormap = OHLCV[:,1] < OHLCV[:,4]
color = np.zeros(colormap.__len__(), dtype = np.dtype('|S5'))
color[:] = 'red'
color[np.where(colormap)] = 'green'
dates = date2num( OHLCV[:,0])
lines_hl = LineCollection( zip(zip(dates, OHLCV[:,2]), zip(dates, OHLCV[:,3])))
lines_hl.set_color(color)
lines_hl.set_linewidth(bar_width)
lines_op = LineCollection( zip(zip((np.array(dates) - date_offset).tolist(), OHLCV[:,1]), zip((np.array(dates)).tolist(), parsed_table[:,1])))
lines_op.set_color(color)
lines_op.set_linewidth(bar_width)
lines_cl = LineCollection( zip(zip((np.array(dates) + date_offset).tolist(), OHLCV[:,4]), zip((np.array(dates)).tolist(), parsed_table[:,4])))
lines_cl.set_color(color)
lines_cl.set_linewidth(bar_width)
ax.add_collection(lines_hl, autolim=True)
ax.add_collection(lines_cl, autolim=True)
ax.add_collection(lines_op, autolim=True)
ax.xaxis.set_major_locator(month)
ax.xaxis.set_major_formatter(timeFmt)
ax.xaxis.set_minor_locator(day)
ax.autoscale_view()
ax.xaxis.grid(True, 'major')
ax.grid(True)
ax.set_title('EOD test plot')
ax.set_xlabel('Date')
ax.set_ylabel('Price , $')
fig.savefig('test.png', dpi = 50, bbox_inches='tight')
close()
if __name__=='__main__':
data_table = urllib2.urlopen(r"http://ichart.finance.yahoo.com/table.csv?s=IBM&a=00&b=1&c=2012&d=00&e=15&f=2013&g=d&ignore=.csv").readlines()[1:][::-1]
parsed_table = []
#Format: Date, Open, High, Low, Close, Volume
dtype = (lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').date(),float, float, float, float, int)
for row in data_table:
field = row.strip().split(',')[:-1]
data_tmp = [i(j) for i,j in zip(dtype, field)]
parsed_table.append(data_tmp)
parsed_table = np.array(parsed_table)
import time
bf = time.time()
count = 100
for i in xrange(count):
test_plot(parsed_table)
print('Plot time: %s' %(time.time() - bf) / count)
The result is something like this. Average time execution on each plot is aproximately 2.6s. Charting in R is much faster, but I didn't measure the performance and I don't want use Rpy, so I bielive that my code is inefficient.
This solution reuses a Figure instance and saves plots asynchronously. You could change this to have as many figures as there are processors, do that many plots asynchronously, and it should speed things up even more. As it is, this takes ~1s per plot, down from 2.6 on my machine.
import numpy as np
import datetime
import urllib2
import time
import multiprocessing as mp
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.collections import LineCollection
class AsyncPlotter():
def __init__(self, processes=mp.cpu_count()):
self.manager = mp.Manager()
self.nc = self.manager.Value('i', 0)
self.pids = []
self.processes = processes
def async_plotter(self, nc, fig, filename, processes):
while nc.value >= processes:
time.sleep(0.1)
nc.value += 1
print "Plotting " + filename
fig.savefig(filename)
plt.close(fig)
nc.value -= 1
def save(self, fig, filename):
p = mp.Process(target=self.async_plotter,
args=(self.nc, fig, filename, self.processes))
p.start()
self.pids.append(p)
def join(self):
for p in self.pids:
p.join()
class FinanceChart():
def __init__(self, async_plotter):
self.async_plotter = async_plotter
self.bar_width = 1.3
self.date_offset = 0.5
self.fig = plt.figure(figsize=(50, 20), facecolor='w')
self.ax = self.fig.add_subplot(1, 1, 1)
self.labels = self.ax.get_xmajorticklabels()
setp(self.labels, rotation=0)
line_hl = LineCollection(([[(734881,1), (734882,5), (734883,9), (734889,5)]]))
line_op = LineCollection(([[(734881,1), (734882,5), (734883,9), (734889,5)]]))
line_cl = LineCollection(([[(734881,1), (734882,5), (734883,9), (734889,5)]]))
self.lines_hl = self.ax.add_collection(line_hl, autolim=True)
self.lines_op = self.ax.add_collection(line_cl, autolim=True)
self.lines_cl = self.ax.add_collection(line_op, autolim=True)
self.ax.set_title('EOD test plot')
self.ax.set_xlabel('Date')
self.ax.set_ylabel('Price , $')
month = MonthLocator()
day = DayLocator()
timeFmt = DateFormatter('%Y-%m-%d')
self.ax.xaxis.set_major_locator(month)
self.ax.xaxis.set_major_formatter(timeFmt)
self.ax.xaxis.set_minor_locator(day)
def test_plot(self, OHLCV, i):
colormap = OHLCV[:,1] < OHLCV[:,4]
color = np.zeros(colormap.__len__(), dtype = np.dtype('|S5'))
color[:] = 'red'
color[np.where(colormap)] = 'green'
dates = date2num( OHLCV[:,0])
date_array = np.array(dates)
xmin = min(dates)
xmax = max(dates)
ymin = min(OHLCV[:,1])
ymax = max(OHLCV[:,1])
self.lines_hl.set_segments( zip(zip(dates, OHLCV[:,2]), zip(dates, OHLCV[:,3])))
self.lines_hl.set_color(color)
self.lines_hl.set_linewidth(self.bar_width)
self.lines_op.set_segments( zip(zip((date_array - self.date_offset).tolist(), OHLCV[:,1]), zip(date_array.tolist(), OHLCV[:,1])))
self.lines_op.set_color(color)
self.lines_op.set_linewidth(self.bar_width)
self.lines_cl.set_segments( zip(zip((date_array + self.date_offset).tolist(), OHLCV[:,4]), zip(date_array.tolist(), OHLCV[:,4])))
self.lines_cl.set_color(color)
self.lines_cl.set_linewidth(self.bar_width)
self.ax.set_xlim(xmin,xmax)
self.ax.set_ylim(ymin,ymax)
self.ax.xaxis.grid(True, 'major')
self.ax.grid(True)
self.async_plotter.save(self.fig, '%04i.png'%i)
if __name__=='__main__':
print "Starting"
data_table = urllib2.urlopen(r"http://ichart.finance.yahoo.com/table.csv?s=IBM&a=00&b=1&c=2012&d=00&e=15&f=2013&g=d&ignore=.csv").readlines()[1:][::-1]
parsed_table = []
#Format: Date, Open, High, Low, Close, Volume
dtype = (lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').date(),float, float, float, float, int)
for row in data_table:
field = row.strip().split(',')[:-1]
data_tmp = [i(j) for i,j in zip(dtype, field)]
parsed_table.append(data_tmp)
parsed_table = np.array(parsed_table)
import time
bf = time.time()
count = 10
a = AsyncPlotter()
_chart = FinanceChart(a)
print "Done with startup tasks"
for i in xrange(count):
_chart.test_plot(parsed_table, i)
a.join()
print('Plot time: %.2f' %(float(time.time() - bf) / float(count)))