[GIS] Plotting “large” image with points with matplotlib – MemoryError

matplotlibnumpypython-2.7

I want to plot an image of 3193 x 3293 pixels with 1502 points. When running in a smaller image and less points (934,722 pixels and 60 points), everything runs perfectly.

I wrote like this:

# I am just giving the general characteristics of the data;    
img = numpy.ndarray, dtype = uint8, shape = (3193,3293) 
points = numpy.ndarray, dtype = int32, shape = (1502,2)

plt.imshow(img, cmap=plt.cm.gray_r)
plt.autoscale(False)
plt.plot(points[:, 1], points[:, 0], 'r.', markersize = 15)
plt.axis('off')
plt.show()

Is there any other way to do this without using that much memory?

Best Answer

matplotlib.imshow() can only plot grayscales if they are of dtype float. Your memory error is most likely due to an internal copy, which changes your dtype from uint8 to float32/64.

Do you have enough memory to drectly work with img = numpy.ndarray, dtype = float32? If not your best bet is most likely to write the image directly to disc, using for instance PIL, which should be able to take dtype = uint8 arrays as input.