import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as ani
#放物線
def f(x):
y = x**2
return y
#コメントの挿入
def interlude(imgs,ax,text,frames):
fdic = {"ha" : "center",
"va" : "center",
"size" : 20,
"color" : "white",
"fontfamily": "Meiryo",
}
boxdic = {"facecolor" : "black",
"pad" : 300,
}
for i in range(frames):
interlude=ax.text(0,1,text,
fontdict=fdic,bbox=boxdic)
imgs.append([interlude])
#十字マークをプロット
def plot_xmark(ax,x):
slope = 2*x
rad = np.arctan(slope)
deg = np.rad2deg(rad)
P = ax.plot(x,f(x),marker=(4,2,deg),
color="r",markersize=20,markeredgewidth=4)
return P
#図の準備
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(-2,2)
ax.set_ylim(-1,3)
ax.axhline(0,color='0.6')
ax.plot(2,0,marker=5,color='0.6')
ax.axvline(0,color='0.6')
ax.plot(0,3,marker=6,color='0.6')
ax.set_aspect("equal")
ax.axis("off")
ax.set_title("2020京大数学 文系第2問",pad=15)
x = np.arange(-2,2.05,0.05)
y = x**2
ax.plot(x,y,color="0.3",linewidth=4)
#変数の準備
A = np.arange(0.1,np.sqrt(3),0.05)
B = np.arange(-2*np.sqrt(3),2*np.sqrt(3),0.2)
imgs = []
#1つ目のパターン
for a in A:
y1 = -1/(4*a**2)*x**2 + a**2 + 1/4
C1 = ax.plot(x,y1,color="0.3",linewidth=4)
P1 = plot_xmark(ax,-a)
P2 = plot_xmark(ax, a)
imgs.append(C1+P1+P2)
comment='''\
えっ (;゚д゚)
これだけじゃないの??'''
interlude(imgs,ax,comment,20)
#2つ目のパターン
for b in B:
y2 = -x**2 + b*x + 1/2
C2 = ax.plot(x,y2,color="0.3",linewidth=4)
alpha1 = (b+np.sqrt(b**2+4))/4
alpha2 = (b-np.sqrt(b**2+4))/4
P1 = plot_xmark(ax,alpha1)
P2 = plot_xmark(ax,alpha2)
imgs.append(C2+P1+P2)
comment='''\
そのパターンもあるのネ
(;´Д`) トホホ'''
interlude(imgs,ax,comment,20)
mov=ani.ArtistAnimation(fig, imgs, 100)
plt.show()