Ask Your Question

# Revision history [back]

### Plot shared axis

I was trying to convert this into sage. Basically print function, first derivative, second derivative all sharing the x axis. Instead of scipy.misc.derivative I'd like to use sage derivative(). I have tries for hours an got several different errors. Any ideas?

d={}
with open(source) as f:
for line in f:
(key, val) = line.split()
d[str(key)] = val
print key
f = lambda t : eval(val)
x = np.arange(1,20,0.01)
first = derivative(f,x,dx=1,n=1)
second = derivative(f,x,dx=1,n=2)
fig,ax = plt.subplots(3,1,sharex=True)
#plt.suptitle("\n".join(wrap(str(key + " - \n" + val), 50)))
ax[0].plot(x,f(x))
ax[0].set_ylabel(r'$f(x)$')
ax[1].plot(x,first)
ax[1].set_ylabel(r'$f\/\prime(x)$')
ax[2].plot(x,second)
ax[2].set_ylabel(r'$f\/\prime\prime(x)$')
ax[2].set_xlabel(r'$x$')
plt.savefig()
plt.close()


The results should be something like this (i would upload my own image but I realized I cannot): http://matplotlib.org/_images/subplots_demo_03.png