I have an instrument that continuously measures voltage. By calling get_sample_r
, I am appending the voltage value measured at a given time to v_measured_array
, which I would like to visualize as a 2D color plot in real time. Below is my attempt at it using matplotlib
.
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
signal_path="/dev6541/demods/0/sample"
v_range= [[0,15], [0,15]]
npoints_x=20
npoints_y=20
n_point_x = np.linspace(v_range[0][0], v_range[0][1], npoints_x)
n_point_y = np.linspace(v_range[1][0], v_range[1][1], npoints_y)
v_measured_array = np.ones(shape=(npoints_x,npoints_y))
plt.show(block=False)
fig = plt.figure()
for each_x in range(0,npoints_x):
for each_y in range(0,npoints_y):
v_measured_array[each_x][each_y]= get_sample_r(daq_1, signal_path)
ax = fig.add_subplot(111)
ax.imshow(v_measured_array, cmap='Greens', interpolation='None')
fig.canvas.draw()
fig.canvas.flush_events()
Although the code above does work in principle, I realized it gets impossibly slow for a large 2D array because I am replotting the entirety of v_measured_array
.
I have two questions:
Is there a way to add only the newly acquired data point (pixel) to the existing plot rather than updating the plot by replotting the entire data set?
I noticed that my plot always gets frozen after the loop is over. I have tried many different backend values
['GTK3Agg', 'GTK3Cairo', 'GTK4Agg', 'GTK4Cairo', 'MacOSX', 'nbAgg', 'QtAgg', 'QtCairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
None of them solved the problem. (I use Spyder as my Python IDE)