本文主要是介绍先睹为快_Mandelbrot集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文引用自作者编写的下述图书; 本文允许以个人学习、教学等目的引用、讲授或转载,但需要注明原作者"海洋饼干叔
叔";本文不允许以纸质及电子出版为目的进行抄摘或改编。
1.《Python编程基础及应用》,陈波,刘慧君,高等教育出版社。免费授课视频 Python编程基础及应用
2.《Python编程基础及应用实验教程》, 陈波,熊心志,张全和,刘慧君,赵恒军,高等教育出版社Python编程基础及应用实验教程
3. 《简明C及C++语言教程》,陈波,待出版书稿。免费授课视频
先睹为快_Mandelbrot集
简易版本的Mandelbrot集
"""
Draw mandelbrot set with matplotlib.
Matrix generated by Pure Python code.
chenbo@cqu.edu.cn, Dec 2018.Usage:key-up, key-down: switch color map
"""from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as npdef getEscapeTime(c):"计算参数c的逃逸时间,该逃逸速度将用作点的颜色"z = 0for i in range(100):if abs(z) > 2:return iz = z*z + creturn idef computeMandelbrot(xCenter,yCenter,semiWidth,N):xFrom,xTo,yFrom,yTo = xCenter-semiWidth,xCenter+semiWidth,\yCenter-semiWidth,yCenter+semiWidthy,x = np.ogrid[yFrom:yTo:N*1j,xFrom:xTo:N*1j]c = x + y*1jprint("c.shape:",c.shape,"x.shape:",x.shape,"y.shape:",y.shape)return np.frompyfunc(getEscapeTime,1,1)(c).astype(np.float)def drawMandelbrot(ax,xCenter,yCenter,semiWidth,N,cmap):"(xCenter,yCenter)-中心点,semiWidth-矩形半宽,N*N像素."ax.set_axis_off()ds = computeMandelbrot(xCenter,yCenter,semiWidth,N)ax.imshow(ds,cmap=cmap)def refresh():c = cm.get_cmap(para.cmaps[para.idxColorMap%len(para.cmaps)])drawMandelbrot(para.ax0, para.x, para.y,semiWidth=0.2, N=600,cmap=c)drawMandelbrot(para.ax1, para.x, para.y,semiWidth=0.2 ** 3, N=600,cmap=c)para.fig.canvas.draw()def on_key_release(event):if event.key == 'up':para.idxColorMap+=1elif event.key == 'down':para.idxColorMap-=1else:returnrefresh()class Para:passpara = Para()
para.x, para.y = 0.27322626, 0.595153338
para.idxColorMap = 0
para.cmaps = ['rainbow', 'jet', 'nipy_spectral', 'gist_ncar','flag','prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern','gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg','gist_rainbow']
para.fig = plt.figure(figsize=(12,6),dpi=100)
para.fig.canvas.mpl_connect('key_release_event',on_key_release)
plt.subplots_adjust(0,0,1,1,0.0,0)
para.ax0 = plt.subplot(121)
para.ax1 = plt.subplot(122)
refresh()
plt.show()
交互版本的Mandelbrot集
"""
Draw mandelbrot set with matplotlib.
Escape time computed by Cython.
chenbo@cqu.edu.cn, Dec 2018.Usage:left-click mouse: Zoom inright-click mouse: Zoom outkey escape: Resetkey up,key down: Switch color map.
"""from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np
from MandelbrotComp import getEscapeTimedef computeMandelbrot(xCenter,yCenter,semiWidth,N):xFrom,xTo,yFrom,yTo = xCenter-semiWidth,xCenter+semiWidth,\yCenter-semiWidth,yCenter+semiWidthy,x = np.ogrid[yFrom:yTo:N*1j,xFrom:xTo:N*1j]c = x + y*1jprint("c.shape:",c.shape,"x.shape:",x.shape,"y.shape:",y.shape)return np.frompyfunc(getEscapeTime,1,1)(c).astype(np.float)def drawMandelbrot(ax,xCenter,yCenter,semiWidth,N,cmap):"(xCenter,yCenter)-中心点,semiWidth-矩形半宽,N*N像素."ax.set_axis_off()ds = computeMandelbrot(xCenter,yCenter,semiWidth,N)ax.imshow(ds,cmap=cmap)def refresh():print("x,y =",para.x,para.y,"semiWidth =", para.semiWidth)c = cm.get_cmap(para.cmaps[para.idxColorMap%len(para.cmaps)])drawMandelbrot(para.ax, para.x, para.y,para.semiWidth, N=700,cmap=c)para.fig.canvas.draw()def on_key_release(event):if event.key == 'up':para.idxColorMap+=1elif event.key == 'down':para.idxColorMap-=1elif event.key == 'escape':para.x, para.y = -0.5, 0.0para.semiWidth = 1.5else:returnrefresh()def on_button_release(event):para.x = (para.x - para.semiWidth) + \2*para.semiWidth*event.xdata/para.figWidthpara.y = (para.y - para.semiWidth) + \2*para.semiWidth*event.ydata/para.figHeightif event.button == 1:para.semiWidth /= 3.0elif event.button == 3:para.semiWidth *= 3.0refresh()class Para:passpara = Para()
para.x, para.y = -0.5, 0.0
para.semiWidth = 1.5
para.idxColorMap = 0
para.cmaps = ['rainbow', 'jet', 'nipy_spectral', 'gist_ncar','flag','prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern','gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg','gist_rainbow']
para.fig = plt.figure(figsize=(7,7),dpi=100)
para.figWidth,para.figHeight = 700,700
para.fig.canvas.mpl_connect('key_release_event',on_key_release)
para.fig.canvas.mpl_connect('button_release_event',on_button_release)
plt.subplots_adjust(0,0,1,1,0.0,0)
para.ax = plt.subplot(111)
refresh()
plt.show()
这个需要Cython的支持,使用Cython语言编写一个核心函数,然后将其编译成Python模块(MandelbrotComp )来调用。
def getEscapeTime(complex c):"计算参数c的逃逸时间,该逃逸速度将用作点的颜色"cdef complex z = 0cdef int ifor i in range(100):if z.real * z.real + z.imag*z.imag > 4:return iz = z*z + creturn i
上面这个代码跟普通的Python不一样,里面的变量是有类型声明的,Cython可以将其编译成.c文件,然后再编译成Python模块。
为了帮助更多的年轻朋友们学好编程,作者在B站上开了两门免费的网课,一门零基础讲Python,一门零基础C和C++一起学,拿走不谢!
简洁的C及C++
Python编程基础及应用
如果你觉得纸质书看起来更顺手,目前Python有两本,C和C++在出版过程中。
Python编程基础及应用
Python编程基础及应用实验教程
这篇关于先睹为快_Mandelbrot集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!