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.plot(points[:, 1], points[:, 0], 'r.', markersize = 15)

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


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.

  • Thanks for the answer Kersten. That is exactly what is happening. I am having a problem with a internal copy. Currently I have 16GB of memory. The problem is that I have to work with float data. Each pixel corresponds to a height value. If I use uint8 I will have problems with precision. – João Paulo Pereira Jul 20 '15 at 11:04
  • If you are working with floats no copy should happen, but you specified img = numpy.ndarray, dtype = uint8, shape = (3193,3293). I am confused, which dtype is your dataset? – Kersten Jul 20 '15 at 12:05
  • Sorry for the confusion. First a tried using uint8 but then I realized I could not use it because of the precision. Then I tried using float format. But I still have a MemoryError. – João Paulo Pereira Jul 25 '15 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.