阴影和增强标准化渲染
通过使用与幂归一化色图(gamma = 0.3)相关联的归一化重新计数,可以改善Mandelbrot集渲染。 由于阴影,渲染可以进一步增强。
maxiter给出了计算的精度。 在大多数现代笔记本电脑上,maxiter = 200应该需要几秒钟。

import numpy as npdef mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0):X = np.linspace(xmin, xmax, xn).astype(np.float32)Y = np.linspace(ymin, ymax, yn).astype(np.float32)C = X + Y[:, None] * 1jN = np.zeros_like(C, dtype=int)Z = np.zeros_like(C)for n in range(maxiter):I = np.less(abs(Z), horizon)N[I] = nZ[I] = Z[I]**2 + C[I]N[N == maxiter-1] = 0return Z, Nif __name__ == '__main__':import timeimport matplotlibfrom matplotlib import colorsimport matplotlib.pyplot as pltxmin, xmax, xn = -2.25, +0.75, 3000/2ymin, ymax, yn = -1.25, +1.25, 2500/2maxiter = 200horizon = 2.0 ** 40log_horizon = np.log(np.log(horizon))/np.log(2)Z, N = mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon)# Normalized recount as explained in:# https://linas.org/art-gallery/escape/smooth.html# https://www.ibm.com/developerworks/community/blogs/jfp/entry/My_Christmas_Gift# This line will generate warnings for null values but it is faster to# process them afterwards using the nan_to_numwith np.errstate(invalid='ignore'):M = np.nan_to_num(N + 1 -np.log(np.log(abs(Z)))/np.log(2) +log_horizon)dpi = 72width = 10height = 10*yn/xnfig = plt.figure(figsize=(width, height), dpi=dpi)ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)# Shaded renderinglight = colors.LightSource(azdeg=315, altdeg=10)M = light.shade(M, cmap=plt.cm.hot, vert_exag=1.5,norm=colors.PowerNorm(0.3), blend_mode='hsv')plt.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic")ax.set_xticks([])ax.set_yticks([])# Some advertisement for matplotlibyear = time.strftime("%Y")text = ("The Mandelbrot fractal set\n""Rendered with matplotlib %s, %s - http://matplotlib.org"% (matplotlib.__version__, year))ax.text(xmin+.025, ymin+.025, text, color="white", fontsize=12, alpha=0.5)plt.show()
Total running time of the script: ( 0 minutes 4.800 seconds)
