问题
I am quite new to python and am trying to plot data using pcolormesh, and have to create many images in a loop with quite large arrays. The code to generate the data works fine and creates the arrays, however the problem arises when I try to plot. It seems that the first iteration creates the image fine, however the second has a memory error unlike the usual ones I've seen before. This one comes from the column_stack function apparently multiplying my x and y arrays together. I'm not entirely sure why it would do this, I've tried closing the figure and deleting all variables before the next iteration, however the problems still there. Heres an example of my function within the loop for plotting the data:
import numpy as np
import matplotlib.pyplot as plt
import pathlib
import sys
import os
from scipy import stats
def dataplot(x, y, time, outputfolder, filenumber, gamma, colormap):
clrmap = plt.get_cmap(colormap)
xum = x * 1e6 # convert to microns
yum = y * 1e6
fig = plt.figure()
cax = plt.pcolormesh(xum, yum, gamma.T, cmap=clrmap)
cbar = plt.colorbar(cax, orientation='vertical')
cbar.set_label('$\gamma$')
plt.xlabel('x/$\mu$m')
plt.ylabel('y/$\mu$m')
plt.title('gamma ' + time + 'ps')
plt.tight_layout()
plt.savefig(outputfolder + filenumber + 'GammaGrid_' +'.png', dpi=300)
# delete everything after:
plt.clf()
plt.close(fig)
del x
del xum
del y
del yum
del gamma
Where:
>>> gamma.shape
(26000,3000)
>>> xum.shape
(26000,)
>>> yum.shape
(3000,)
And the following error arises on the second loop iteration:
Traceback (most recent call last):
File "gamma_grid.py", line 245, in <module>
dataplot(x,y,time,outputfolder,sdffilenumber, gamma_grid, colormap, stat)
File "gamma_grid.py", line 153, in dataplot
cax = plt.pcolormesh(xum, yum, gamma.T, cmap=clrmap)
File "/users/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2758, in pcolormesh
**({"data": data} if data is not None else {}), **kwargs)
File "/users/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py", line 1599, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/users/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 6176, in pcolormesh
coords = np.column_stack((X, Y)).astype(float, copy=False)
MemoryError: Unable to allocate 1.16 GiB for an array with shape (78000000, 2) and data type float64
The shape of the array in the memory error is the same size as xum.shape * yum.shape
, and I'm not quite sure why it is doing that. Any help would be greatly appreciated.
来源:https://stackoverflow.com/questions/61727642/pcolormesh-or-column-stack-function-has-memory-error-when-i-try-creating-images