Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to plot on a smaller scale

I am using matplotlib and I'm finding some problems when trying to plot large vectors. sometimes get "MemoryError" My question is whether there is any way to reduce the scale of values ​​that i need to plot ?

enter image description here

In this example I'm plotting a vector with size 2647296!

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

like image 383
ederwander Avatar asked Dec 27 '22 03:12

ederwander


1 Answers

It is very unlikely that you have so much resolution on your display that you can see 2.6 million data points in your plot. A simple way to plot less data is to sample e.g. every 1000th point: plot(x[::1000]). If that loses too much and it is e.g. important to see the extremal values, you could write some code to split the long vector into suitably many parts and take the minimum and maximum of each part, and plot those:

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))
like image 192
Jouni K. Seppänen Avatar answered Dec 30 '22 03:12

Jouni K. Seppänen