I am using matplotlib to plot a graph with the points ([0,0,0],[0,0,1],[0,0,2],…[255,255,255]) on x-axis for that i am using list:
from mpl_toolkits.mplot3d import Axes3D
x=[]
for i,j,k in product(xrange(256), repeat=3):
x.append([i,j,k])
y=[]
for count in x:
y.append(probability[count]) # this is how my probability array is stored
pylab.figure(0)
pylab.plot(x,y,'b')
pylab.show()
This idea I have borrowed from previous posts. I am new to python, so please help. The question is the above code gives “Memory Error”. Can someone provide an efficient way to append elements to ‘x’
First, I don’t think pylab.plot does what you think it does, are you trying to display a surface in 3d?
Second, you should really be using ndarrays and not lists for something this big. I believe matplotlib will convert your lists to ndarrays anyway so you’re better off starting with arrays. I think something like the following is what you want.
And last, what is
yand what isprobability? I ask becauseprobability[x[count]]looks highly suspect to me, I think maybe you meantprobability[count]but even so, ifprobabilityis a list, that should not work and if it is an array it’ll blow up and could be causing your memory error. (Can’t know for sure without the trace).Take a look at the Matplotlib Gallery, their examples come with code and are very helpful for getting things working.