Try using StringIO
to avoid writing any file-like object to disk.
import matplotlib.pyplot as plt
import StringIO
from matplotlib import numpy as np
x = np.arange(0,np.pi*3,.1)
y = np.sin(x)
fig = plt.figure()
plt.plot(x,y)
imgdata = StringIO.StringIO()
fig.savefig(imgdata, format='svg')
imgdata.seek(0) # rewind the data
svg_dta = imgdata.buf # this is svg data
file('test.htm', 'w').write(svg_dta) # test it
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…