您好,登錄后才能下訂單哦!
matplotlib的Show面板中提供了放大、移動等交互式操作,但也未能涵蓋所有的交互需求,比如希望通過mandelbrot集上的一點來生成對應(yīng)的Julia集。
Julia集
Julia可以說是分形鼻祖,指的是對于給定的一個復(fù)數(shù)ccc,使得迭代式f(z)=z2+cf(z)=z^2+cf(z)=z2+c收斂的復(fù)數(shù)zzz的集合。例如,當(dāng)c=0c=0c=0時,那么其收斂區(qū)間為z2<1z^2<1z2<1的單位圓,對應(yīng)的ccc的Julia集便是cos?θ+isin?θ\cos\theta+i\sin\thetacosθ+isinθ。
特別地,當(dāng)c=zc=zc=z的初始值時,符合收斂條件的zzz的便構(gòu)成大名鼎鼎的Mandelbrot集
它的圖的顏色表示該點的發(fā)散速度,可以理解為開始發(fā)散時迭代的次數(shù)。其生成代碼也非常簡單:
#mbrot.py
import numpy as np
import time
import pyplotlib.pyplot as plt
#生成z坐標(biāo),axis為起始位置,nx,ny為x向和y向的格點個數(shù)
def genZ(axis,nx,ny):
x0,x1,y0,y1 = axis
x = np.linspace(x0,x1,nx)
y = np.linspace(y0,y1,ny)
real, img = np.meshgrid(x,y)
z = real + img*1j
return z
#獲取Julia集,n為迭代次數(shù),m為判定發(fā)散點,大于1即可
def getJulia(z,c,n,m=2):
t = time.time()
c = np.zeros_like(z)+c
out = abs(z)
for i in range(n):
absz = abs(z)
z[absz>m]=0 #對開始發(fā)散的點置零
c[absz>m]=0
out[absz>m]=i #記錄發(fā)散點的發(fā)散速度
z = z*z + c
print("time:",time.time()-t)
return out
if __name__ == "__main__":
axis = np.array([-2,1,-1.5,1.5])
z0 = genZ(axis,500,500)
mBrot = getJulia(z0,z0,50)
plt.imshow(mBrot, cmap=cm.jet, extent=axis)
plt.gca().set_axis_off()
plt.show()
matplotlib綁定事件
下面希望實現(xiàn)點擊Mandelbrot集中的一點,生成相應(yīng)的Julia集。
在mpl中,事件綁定函數(shù)mpl_connect被封裝在cavnas類中,調(diào)用格式為canvas.mpl_connect('str', func),其中func事件函數(shù),字符串為被傳入事件函數(shù)的事件標(biāo)識,如下所列,望文生義即可
'button_press_event'
'button_release_event'
'draw_event'
'key_press_event'
'key_release_event'
'motion_notify_event'
'pick_event'
'resize_event'
'scroll_event'
'figure_enter_event'
'figure_leave_event'
'axes_enter_event'
'axes_leave_event'
'close_event'
簡單起見,可以先檢測一下鼠標(biāo)點擊事件'button_press_event',對此我們需要定義一個事件函數(shù),并將上面的入口函數(shù)稍加修改:
def test(evt):
print(evt.xdata) #xdata即x方向的坐標(biāo)
if __name__ == "__main__":
axis = np.array([-2,1,-1.5,1.5])
z0 = genZ(axis,500,500)
mBrot = getJulia(z0,z0,50)
fig, ax = plt.subplots()
fig.canvas.mpl_connect('button_press_event', test)#調(diào)用事件函數(shù)
plt.imshow(mBrot, cmap=cm.jet, extent=axis)
plt.gca().set_axis_off()
plt.show()
于是點擊imshow()出來的圖片,即可返回相應(yīng)的x坐標(biāo)。
python mbrot.py
time: 0.47572827339172363
-0.8652597402597402
-0.7840909090909087
-0.18344155844155807
0.23051948051948123
0.8149350649350655
縮放
那么生成Julia集只需要重新調(diào)用一次getJulia這個函數(shù)即可。
Mandelbrot集的分形特征意味著我們所生成的圖片可以無限放大,但是mpl自帶的放大工具并不會重新生成數(shù)據(jù),所以是虛假的放大。因此需要重新綁定放大操作,其思路是,當(dāng)右鍵點擊(‘button_press_event’)時,記錄此時的坐標(biāo),當(dāng)右鍵釋(‘button_release_event’)放時重新繪制圖片,為了防止與左鍵沖突,所以在點擊所對應(yīng)的事件函數(shù)中加入左右鍵判斷。其結(jié)果如圖
此外,還可以綁定鼠標(biāo)滾輪,實現(xiàn)Mandelbrot集在該點的真實縮放,代碼如下
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
import matplotlib.backend_bases as mbb
import time
class MandelBrot():
def __init__(self,x0,x1,y0,y1,n):
self.oriAxis = np.array([x0,x1,y0,y1]) #初始坐標(biāo)
self.axis = self.oriAxis
self.nx,self.ny,self.nMax = n,n,n #x,y方向的網(wǎng)格劃分個數(shù)
self.nIter = 100 #迭代次數(shù)
self.n0 = 0 #預(yù)迭代次數(shù)
self.z = genZ(self.oriAxis,self.nx,self.ny)
self.DrawMandelbrot()
def DrawMandelbrot(self):
mBrot = getJulia(self.z,self.z,self.nIter)
self.fig, ax = plt.subplots()
plt.imshow(mBrot, cmap=cm.jet, extent=self.axis)
plt.gca().set_axis_off()無錫婦科醫(yī)院排行 http://mobile.wxbhnkyy39.com/
self.fig.canvas.mpl_disconnect(self.fig.canvas.manager.key_press_handler_id)
self.fig.canvas.mpl_connect('button_press_event', self.OnMouse)
self.fig.canvas.mpl_connect('button_release_event', self.OnRelease)
self.fig.canvas.mpl_connect('scroll_event', self.OnScroll)
plt.show()
def DrawJulia(self,c0):
z = genZ([-2,2,-2,2],800,800)
julia = getJulia(z,c0,self.nIter)
jFig,jAx = plt.subplots()
plt.cla()
plt.imshow(julia, cmap=cm.jet, extent=self.axis)
plt.gca().set_axis_off()
plt.show()
jFig.canvas.draw_idle()
#滾輪縮放
def OnScroll(self,evt):
x0,y0 = evt.xdata,evt.ydata
if evt.button == "up":
self.axis = (self.axis+[x0,x0,y0,y0])/2
elif evt.button == 'down':
self.axis = 2*self.axis-[x0,x0,y0,y0]
z = genZ(self.axis,self.nx,self.ny)
mBrot = getJulia(z,z,self.nIter)
plt.cla()
plt.imshow(mBrot, cmap=cm.jet, extent=self.axis)
plt.gca().set_axis_off()
mBrot[mBrot<1]==self.n0+self.nIter
self.n0 = int(np.min(mBrot))
self.fig.canvas.draw_idle()
pass
def OnMouse(self, evt):
self.xStart = evt.xdata
self.yStart = evt.ydata
self.fig.canvas.draw_idle()
def OnRelease(self,evt):
x0,y0,x1,y1 = self.xStart,self.yStart,evt.xdata,evt.ydata
if evt.button == mbb.MouseButton.LEFT:
self.DrawJulia(x1+y1*1j) #如果釋放的是左鍵,那么就繪制Julia集并返回
return
#右鍵拖動,可以對Mandelbrot集進(jìn)行真實的放大
self.axis = np.array([min(x0,x1),max(x0,x1),
min(y0,y1),max(y0,y1)])
nxny = self.axis[[1,3]]-self.axis[[0,2]]
self.nx,self.ny = (nxny/max(nxny)*self.nMax).astype(int)
z = genZ(self.axis,self.nx,self.ny)
n = 100 #n為迭代次數(shù)
mBrot = getJulia(z,z,n)
plt.cla()
plt.imshow(mBrot, cmap=cm.jet, extent=self.axis)
plt.gca().set_axis_off()
mBrot[mBrot<1]==self.n0+n
self.n0 = int(np.min(mBrot))
self.fig.canvas.draw_idle()
def genZ(axis,nx,ny):
x0,x1,y0,y1 = axis
x = np.linspace(x0,x1,nx)
y = np.linspace(y0,y1,ny)
real, img = np.meshgrid(x,y)
z = real + img*1j
return z
def getJulia(z,c,n,n0=0,m=2):
t = time.time()
c = np.zeros_like(z)+c
out = abs(z)
for _ in range(n0):
z = z*z + c
for i in range(n0,n0+n):
absz = abs(z)
z[absz>m]=0
c[absz>m]=0
out[absz>m]=i
z = z*z + c
print("time:",time.time()-t)
return out
if __name__ == "__main__":
x,y = 0,0
brot = MandelBrot(-2,1,-1.5,1.5,1000)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。