# -*- coding: utf-8 -*- # by whyx 2016/6 import numpy as np import pylab as pl from math import log import time from matplotlib import cm def iter_point(c): z = c for i in xrange(1, 100): # 最多迭代100次 if abs(z)>2.0: break # 半径大于2则认为逃逸 z = z*z+c return i # 返回迭代次数 escape_radius = 2.0 iter_num = 100 def smooth_iter_point(c): z = c for i in xrange(1, iter_num): if abs(z)>escape_radius: break z = z*z+c absz = abs(z) if absz > 2.0: mu = i - log(log(abs(z),2),2) else: mu = i return mu # 返回正规化的迭代次数 def draw_mandelbrot(cx, cy, d): # 绘制点(cx, cy)附近正负d的范围的Mandelbrot (x0, x1, y0, y1) = (cx-d, cx+d, cy-d, cy+d) y, x = np.ogrid[y0:y1:200j, x0:x1:200j] c = x + y*1j start = time.clock() mandelbrot = np.frompyfunc(smooth_iter_point,1,1)(c).astype(np.float) print "time=",time.clock() - start #时间单位 秒 pl.imshow(mandelbrot, cmap=cm.Blues_r, extent=[x0,x1,y0,y1]) pl.gca().set_axis_off() x,y = 0.27322626, 0.595153338 pl.subplot(231) draw_mandelbrot(-0.5, 0, 1.5) for i in range(2,7): pl.subplot(230+i) draw_mandelbrot(x, y, 0.2**(i-1)) pl.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0) pl.show()