matplotlib - Python: How do I plot a signal with color-coded values as background? -
i beginner in python , matplotlib, extensive search did not yield useful, here goes:
i have aquired data
(256 samples per seconds) device. code below recreate (and plot) data set.
# import modules import numpy np import matplotlib.pyplot plt # set variable fs = 256 # create list random measurements np.random.seed(1) data = [np.random.uniform(-20000, 20000) in range(10*fs)] #plt.plot(data, color = "black", linewidth = 0.3); plt.show()
for data
, have calculated several values every second, i.e. 256 data points. every second characterized set of these numbers. code below create data set 3 of these numbers, called sig_val
s.
# create lists random sig_values length of 1/fs of measurements sig_vals1 = [np.random.uniform(0, 1) in range(int(len(data)/fs))] sig_vals2 = [np.random.uniform(0, 1) in range(int(len(data)/fs))] sig_vals3 = [np.random.uniform(0, 1) in range(int(len(data)/fs))]
the goal have overlay graph shows
- heatmap-like color-coded information
data
's intervals, - overlayed original
data
.
how nice plot of color-coded values of sig_vals
background of data
?
your problem scale problem... when plot spectrogram x axis in range 0-8 second , y axis in range of fs/2=128, when data in rang of [-20000:20000] y axis , 2560 in x axis... should scale data plot in same graph.
you can :
# import modules import numpy np import matplotlib.pyplot plt # set variable fs = 256 # create list random measurements np.random.seed(1) data = [np.random.uniform(-20000, 20000) in range(10*fs)] #plt.plot(data, color = "black", linewidth = 0.3); plt.show() # create lists random sig_values length of 1/fs of measurements sig_vals1 = [np.random.uniform(0, 1) in range(int(len(data)/fs))] sig_vals2 = [np.random.uniform(0, 1) in range(int(len(data)/fs))] sig_vals3 = [np.random.uniform(0, 1) in range(int(len(data)/fs))]
here how plot overlaid :
plt.specgram(data, fs=fs) #plot spectrogram t=[float(i)/fs in range(10*fs)] #create time vector data2=[((float(i)/40000)+0.5)*fs/2 in data] # scale data between [0:128] overlay spectrogram plt.plot(t, data2, color = "black", lw=0.3) # plot scaled data plt.show()
but maybe it's better create subplot , keep original data :
ax1 = plt.subplot(211) plt.specgram(data, fs=fs) plt.specgram(data, fs=fs) plt.subplot(212 ) plt.plot(t, data, color = "black", lw=0.3) plt.show()
edit : use imshow()
:
tot=[sig_vals1, sig_vals2,sig_vals3] t=[float(i)/fs in range(10*fs)] data2=[((float(i)/40000)+0.5)*2 in data] plt.plot(t, data2, color = "black", lw=0.3) plt.imshow(tot) plt.show()
wiki
Comments
Post a Comment