I am trying to make an interactive plot of the tangent line using matplotlib
and cv2
. Before, I was just saving the files to my disk, then opening the images as .png
into a list of images. I decided to omit using the disk and use a cStringIO
buffer. I've ommitted the image display loop for now because I'm quite certain it will work when I get past this. I have two functions, pic()
generates the plot, then calls convt()
to convert the plot into a buffer, and extracts the raw string using buff.getValue
, then returns the string. The problem seems to be with np.frombuffer()
. If I just print pic()
I can see the buffer as a string, but np.frombuffer(pic())
causes the ValueEerror
:
ValueError: buffer size must be a multiple of element size
Here are the two functions and the commented out loop to fill the list. The math function definitions have been left out because I already tested them and they work as intended. Note, this version is minimized to cause the same error:
from matplotlib import pyplot as plt
import numpy as np
from cStringIO import StringIO
js= lambda t: 3*t #define function
ts=np.linspace(-3,3,100) #define domain
def convt(fig):
buff=StringIO() #instantiate StringIO object
fig.savefig(buff, format='png', dpi=fig.dpi) #print png to buffer
rawstring=buff.getvalue() #pull string from buffer
buff.close() #close buffer
return rawstring #return string
def pic(ts,js):
plt.plot(ts,js(ts)) #generate mpl plot
fig=plt.gcf() #grab current figure
out=convt(fig) #convert to string
plt.close() #close plot
return out #return string
np.frombuffer(rawstring) #causes ValueError, remove np.frombuffer()
#and you can see the string