- 三次元プロット
- ぐるぐる回す
- アニメーション
from IPython.display import HTML,display_html
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.animation as anime
def plot3d(
fname,func,itrot,itshw,itfrz,
xrng=(-1,1),
yrng=(-1,1),
zrng=(-1,1),
nx=100,
ny=100,
fpfrz=lambda i:0,
fps=10,
dpi=100,
color="#FF0000",
lw=0.3,
elv=20,
azm=30
):
fig,ax=plt.subplots(figsize=(6,6),subplot_kw={'projection':'3d'})
def plot(i):
ax.cla()
if (i>=itrot+itshw):
x=np.linspace(xrng[0],xrng[1],nx)
y=np.linspace(yrng[0],yrng[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=color,linewidth=lw)
fpfrz(i-itrot-itshw,ax)
ax.view_init(elev=elv,azim=azm)
ax.set_xlim(xrng[0],xrng[1])
ax.set_ylim(yrng[0],yrng[1])
ax.set_zlim(zrng[0],zrng[1])
elif (i>=itrot):
x=np.linspace(xrng[0],xrng[1],nx)
y=np.linspace(yrng[0],yrng[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=color,linewidth=lw)
ax.set_xlim(xrng[0],xrng[1])
ax.set_ylim(yrng[0],yrng[1])
ax.set_zlim(zrng[0],zrng[1])
ax.view_init(elev=elv,azim=azm,)
else:
x=np.linspace(xrng[0],xrng[1],nx)
y=np.linspace(yrng[0],yrng[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=color,linewidth=lw)
ax.set_xlim(xrng[0],xrng[1])
ax.set_ylim(yrng[0],yrng[1])
ax.set_zlim(zrng[0],zrng[1])
ax.view_init(elev=elv,azim=(360/itrot)*i+azm,)
anim=anime.FuncAnimation(fig,plot,frames=itrot+itshw+itfrz)
anim.save(fname,fps=fps,dpi=dpi)
plt.close(fig)
display_html(HTML(anim.to_jshtml(fps=fps)))
def plot3d_expand(
fname,func,itrot,itshw,itexp,itfrz,
xrng0=(-1,1),
yrng0=(-1,1),
zrng0=(-1,1),
xrng1=(-1,1),
yrng1=(-1,1),
zrng1=(-1,1),
nx=100,
ny=100,
fpfrz=lambda i:0,
fps=10,
dpi=100,
color="#FF0000",
ucolor="#7F7F7F",
lw=0.3,
elv=20,
azm=30
):
fig,ax=plt.subplots(figsize=(6,6),subplot_kw={'projection':'3d'})
def plot(i):
ax.cla()
if (i>=itrot+itexp+itshw*2):
x=np.linspace(xrng1[0],xrng1[1],nx)
y=np.linspace(yrng1[0],yrng1[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=color,linewidth=lw)
fpfrz(i-itrot-itexp-itshw*2,ax)
ax.view_init(elev=elv,azim=azm)
ax.set_xlim(xrng1[0],xrng1[1])
ax.set_ylim(yrng1[0],yrng1[1])
ax.set_zlim(zrng1[0],zrng1[1])
if (i>=itrot+itexp+itshw):
x=np.linspace(xrng1[0],xrng1[1],nx)
y=np.linspace(yrng1[0],yrng1[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=color,linewidth=lw)
ax.view_init(elev=elv,azim=azm)
ax.set_xlim(xrng1[0],xrng1[1])
ax.set_ylim(yrng1[0],yrng1[1])
ax.set_zlim(zrng1[0],zrng1[1])
elif (i>=itrot+itshw):
p=((itrot+itexp+itshw-i)/itexp)
x0,x1=xrng0[0]*p+(1-p)*xrng1[0],xrng0[1]*p+(1-p)*xrng1[1]
y0,y1=yrng0[0]*p+(1-p)*yrng1[0],yrng0[1]*p+(1-p)*yrng1[1]
z0,z1=zrng0[0]*p+(1-p)*zrng1[0],zrng0[1]*p+(1-p)*zrng1[1]
x=np.linspace(x0,x1,nx)
y=np.linspace(y0,y1,ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=ucolor,linewidth=lw)
x=np.linspace(xrng1[0],xrng1[1],nx)
y=np.linspace(yrng1[0],yrng1[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=color,linewidth=lw)
ax.view_init(elev=elv,azim=azm)
ax.set_xlim(x0,x1)
ax.set_ylim(y0,y1)
ax.set_zlim(z0,z1)
elif (i>=itrot):
x=np.linspace(xrng0[0],xrng0[1],nx)
y=np.linspace(yrng0[0],yrng0[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=ucolor,linewidth=lw)
x=np.linspace(xrng1[0],xrng1[1],nx)
y=np.linspace(yrng1[0],yrng1[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=color,linewidth=lw)
ax.set_xlim(xrng0[0],xrng0[1])
ax.set_ylim(yrng0[0],yrng0[1])
ax.set_zlim(zrng0[0],zrng0[1])
ax.view_init(elev=elv,azim=azm,)
else:
x=np.linspace(xrng0[0],xrng0[1],nx)
y=np.linspace(yrng0[0],yrng0[1],ny)
X,Y=np.meshgrid(x,y)
ax.plot_wireframe(X,Y,func(X,Y),color=ucolor,linewidth=lw)
ax.set_xlim(xrng0[0],xrng0[1])
ax.set_ylim(yrng0[0],yrng0[1])
ax.set_zlim(zrng0[0],zrng0[1])
ax.view_init(elev=elv,azim=(360/itrot)*i+azm,)
anim=anime.FuncAnimation(fig,plot,frames=itrot+itshw*2+itexp+itfrz)
anim.save(fname,fps=fps,dpi=dpi)
plt.close(fig)
display_html(HTML(anim.to_jshtml(fps=fps)))
func=lambda x,y:np.exp(-(x/0.5)**2-(y/0.3)**2)*np.sin(x*7)*np.cos(y*10)
def plot(i,ax):
x,y=np.random.uniform(0,0.6),np.random.uniform(-0.2,0.4)
ax.scatter(x,y,func(x,y))
plot3d("a.gif",func,36,5,20,(-1,1),(-1,1),(-1,1),200,200,fpfrz=plot)
plot3d_expand("b.gif",func,36,5,14,20,(-1,1),(-1,1),(-1,1),(0,0.6),(-0.2,0.4),(-0.4,1),200,200,fpfrz=plot)