0

I have two variables which are represented by a Numpy array of size 1000000x1. I would like to use matplotlib to plot both of them in the same plot. However, If I proceed and plot them together the result is unintelligible since I have so many values I cannot really understand a lot. Is there a way that I can downsample and plot a more sparse representation of both variables?

plt.plot(acc1) #acc1 contains 10000000 samples
plt.plot(acc2) #acc2 contains 10000000 samples
plt.show()
Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
  • Not quite sure If I really understand your question... can you give a example for that? And or... just plot `acc1 - acc2` if you want to see the difference. – some_name.py Dec 11 '19 at 11:15

2 Answers2

1

You can try binning your series using numpy.linspace and plt.hist working example below. Related stackoverflow post: Plot two histograms at the same time with matplotlib

import random
import numpy
from matplotlib import pyplot

acc1 = [random.gauss(3,1) for _ in range(10000000)]
acc2 = [random.gauss(4,2) for _ in range(10000000)]

bins = numpy.linspace(-10, 10, 100)

pyplot.hist(acc1, bins, alpha=0.5, label='x')
pyplot.hist(acc2, bins, alpha=0.5, label='y')
pyplot.legend(loc='upper right')
pyplot.show()
  • I think in such way with hist i do not plot what I want though. – Jose Ramon Dec 11 '19 at 11:26
  • So then you need to think about if you want to alter your data (sampling) or if you want to change up your plot. Are you plotting a scatter, two lines or something else? – Nicolai Mons Mogensen Dec 11 '19 at 11:29
  • I can alter my data and keep sub-samples of them. – Jose Ramon Dec 11 '19 at 12:23
  • Then you need to find a sampling strategy. Seems a little out of scope for the question you asked. If you are okay with taking, say every nth element you can do a slice on your array. Example: `sub_acc1 = acc1[0::10]` start at 0 and take every 10th element. https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#basic-slicing-and-indexing – Nicolai Mons Mogensen Dec 11 '19 at 12:28
1

take every x sample as follows

downsample = 100 # 100x times (or every 100th sample)
plt.plot(acc1[::downsample]) #acc1 contains 10000000/downsample samples
plt.plot(acc2[::downsample]) #acc2 contains 10000000/downsample samples
plt.show()
Martin
  • 3,333
  • 2
  • 18
  • 39