2
votes

I'm trying to make a scatter plot consisting of ~6 million points in an attempt to understand some sort of clustering.

When I try to do this in a simple scatter command, matplotlib complains about excessive memory. So I decided to plot 3000 points and then save the figure in .png format, clear the figure, load the saved .png with imread() and then overlay the next 3000 points.

I'm facing some padding issues and I do not understand how they've arisen. My code is a bit long since I'm parsing a lot of text files but below is a sample mockup code that replicates my thinking:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.xlim(0,1000)
plt.ylim(-1000,1000)
plt.scatter(400,500,marker="+",c="r")
plt.gca().set_aspect('equal')
plt.draw()
plt.savefig(r"C:\TMP\fig1.png")
plt.clf()
im = plt.imread(r"C:\TMP\fig1.png")
implot = plt.imshow(im, origin='upper', aspect='equal', extent=[0,1000,-1000,1000], zorder=0)
plt.scatter(600,500,marker="+",c="b")
plt.savefig(r"C:\TMP\fig2.png")
plt.close(fig)

The outcome is something that I don't understand how to interpret. Obviously, I do not understand the relationship between "aspect" and "extent" from imshow(). Can somebody help me with this?

Figure 1

fig1.png

Figure 2

fig2.png

I was expecting both fig1.png and fig2.png to perfectly overlay on top of another.

1

1 Answers

1
votes

I made a memory profile with memory_profiler for a sensible example, for 6M points.

import numpy as np
import time
x = np.random.normal(size=6000000)
y = np.random.normal(size=6000000)

start = time.time()
plt.scatter(x, y, alpha=0.1)
end = time.time() - start
print(end)

out is 30.015294551849365 seconds. It's not terribly slow.

On the other hand, the profile output:

Line #    Mem usage    Increment   Line Contents
================================================
 5   81.738 MiB    0.000 MiB   @profile
 6                             def make_test():
 7  127.516 MiB   45.777 MiB       x = np.random.normal(size=6000000)
 8  173.293 MiB   45.777 MiB       y = np.random.normal(size=6000000)
 9                             
10  282.934 MiB  109.641 MiB       plt.scatter(x, y, alpha=0.1)
11  298.160 MiB   15.227 MiB       plt.savefig('big_plot')

It reaches up to 300 Mb, which is not a memory problem either. The problem is elsewhere, but you should be able to plot ALL the points together.

Finally, the scatter plot:

big_scatter_plot