How to plot on a smaller scale

I am using matplotlib and I find some problems when trying to build large vectors. sometimes get a "MemoryError" My question is, is there a way to scale down the values ​​I need to build?

enter image description here

In this example, I am drawing a 2647296 vector!

Is there a way to plot the same values ​​on a smaller scale?

+4
source share
2 answers

It is very unlikely that there is so much resolution on your display that you can see 2.6 million data points in your plot. An easy way to build less data is to fetch, for example. every 1000 points: plot(x[::1000]) . If this is too much, and this, for example, to see extreme values, you can write some code to divide a long vector into enough parts and take the minimum and maximum of each part and put the following values ​​in them:

 tmp = x[:len(x)-len(x)%1000] # drop some points to make length a multiple of 1000 tmp = tmp.reshape((1000,-1)) # split into pieces of 1000 points tmp = tmp.reshape((-1,1000)) # alternative: split into 1000 pieces figure(); hold(True) # plot minimum and maximum in the same figure plot(tmp.min(axis=0)) plot(tmp.max(axis=0)) 
+9
source

You can use min / max for each data block to subexpress the signal.

The window size should be determined based on how accurately you want to display your signal and / or how large the window is compared to the signal length.

Code example:

 from scipy.io import wavfile import matplotlib.pyplot as plt def value_for_window_min_max(data, start, stop): min = data[start] max = data[start] for i in range(start,stop): if data[i] < min: min = data[i] if data[i] > max: max = data[i] if abs(min) > abs(max): return min else: return max # This will only work properly if window_size divides evenly into len(data) def subsample_data(data, window_size): print len(data) print len(data)/window_size out_data = [] for i in range(0,(len(data)/window_size)): out_data.append(value_for_window_min_max(data,i*window_size,i*window_size+window_size-1)) return out_data sample_rate, data = wavfile.read('<path_to_wav_file>') sub_amt = 10 sub_data = subsample_data(data, sub_amt) print len(data) print len(sub_data) fig = plt.figure(figsize=(8,6), dpi=100) fig.add_subplot(211) plt.plot(data) plt.title('Original') plt.xlim([0,len(data)]) fig.add_subplot(212) plt.plot(sub_data) plt.xlim([0,len(sub_data)]) plt.title('Subsampled by %d'%sub_amt) plt.show() 

Conclusion: enter image description here

+1
source

Source: https://habr.com/ru/post/1403099/


All Articles