I'm just starting with matplotlib. I'm trying to animate a quantum well simulation and I want to superpose two plots, for some reason I can't match the value 0 on both y axes... Here's a picture of what's wrong:
I'll attach the code I'm using to animate it as well, but I warn is a bit messy (the problem just happens in the case V0>0 or V0==0) :
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
fig,ax=plt.subplots()
line,=ax.plot(x,PROB[0,:],"r")
ax2 = ax.twinx()
line2,=ax2.plot(x,V,"b")
fig.text(0.888, 0.84, '$-\infty$')
if V0>0 or V0==0:
ax2.fill_between(x, V, 0,facecolor='b',alpha=0.1)
ax.set_yticks(np.linspace(0,1),5)
else:
ax2.fill_between(x, V, V0,facecolor='b',alpha=0.1)
ax.set_yticks(np.linspace(-1,1),5)
if V0 != 0:
line2.set_ydata(V)
ax2.set_ylabel("$Potencial(eV)$")
ax2.yaxis.set_ticks_position('right')
ax2.get_yaxis().set_tick_params(which='both', direction='out')
ax2.yaxis.set_major_locator(ticker.FixedLocator([0,V0]))
else:
ax2.set_ylabel("$Potencial(eV)$")
ax2.yaxis.set_ticks_position('right')
ax2.get_yaxis().set_tick_params(which='both', direction='out')
ax2.yaxis.set_major_locator(ticker.FixedLocator([0]))
ax.set_yticks(np.linspace(0,1),5)
ax.set_xlabel("$Posició (nm)$")
ax.set_ylabel("$Probabilitat\;\;|\psi(x)|^2\;\;(nm^{-1})$")
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('both')
ax.grid(True)
ax.get_yaxis().set_tick_params(which='both', direction='in')
ax.get_xaxis().set_tick_params(which='both', direction='in')
def animacio(i):
line.set_ydata(PROB[i,:])
return line,
ani=animation.FuncAnimation(fig,animacio,interval=50, frames=Nt,repeat=False)
nom='Evolució_'
ani.save(str(nom)+'['+str(V0)+','+str(L)+','+str(l)+','+str(xi)+','+str(sigmax)+','+str(T)+']'+'.mp4', writer="ffmpeg")
plt.show()
print('Animation saved as: '+str(nom)+'['+str(V0)+','+str(L)+','+str(l)+','+str(xi)+','+str(sigmax)+','+str(T)+']'+'.mp4')