阴影和增强标准化渲染

通过使用与幂归一化色图(gamma = 0.3)相关联的归一化重新计数,可以改善Mandelbrot集渲染。 由于阴影,渲染可以进一步增强。

maxiter给出了计算的精度。 在大多数现代笔记本电脑上,maxiter = 200应该需要几秒钟。

阴影和增强标准化渲染示例

  1. import numpy as np
  2. def mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0):
  3. X = np.linspace(xmin, xmax, xn).astype(np.float32)
  4. Y = np.linspace(ymin, ymax, yn).astype(np.float32)
  5. C = X + Y[:, None] * 1j
  6. N = np.zeros_like(C, dtype=int)
  7. Z = np.zeros_like(C)
  8. for n in range(maxiter):
  9. I = np.less(abs(Z), horizon)
  10. N[I] = n
  11. Z[I] = Z[I]**2 + C[I]
  12. N[N == maxiter-1] = 0
  13. return Z, N
  14. if __name__ == '__main__':
  15. import time
  16. import matplotlib
  17. from matplotlib import colors
  18. import matplotlib.pyplot as plt
  19. xmin, xmax, xn = -2.25, +0.75, 3000/2
  20. ymin, ymax, yn = -1.25, +1.25, 2500/2
  21. maxiter = 200
  22. horizon = 2.0 ** 40
  23. log_horizon = np.log(np.log(horizon))/np.log(2)
  24. Z, N = mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon)
  25. # Normalized recount as explained in:
  26. # https://linas.org/art-gallery/escape/smooth.html
  27. # https://www.ibm.com/developerworks/community/blogs/jfp/entry/My_Christmas_Gift
  28. # This line will generate warnings for null values but it is faster to
  29. # process them afterwards using the nan_to_num
  30. with np.errstate(invalid='ignore'):
  31. M = np.nan_to_num(N + 1 -
  32. np.log(np.log(abs(Z)))/np.log(2) +
  33. log_horizon)
  34. dpi = 72
  35. width = 10
  36. height = 10*yn/xn
  37. fig = plt.figure(figsize=(width, height), dpi=dpi)
  38. ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)
  39. # Shaded rendering
  40. light = colors.LightSource(azdeg=315, altdeg=10)
  41. M = light.shade(M, cmap=plt.cm.hot, vert_exag=1.5,
  42. norm=colors.PowerNorm(0.3), blend_mode='hsv')
  43. plt.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic")
  44. ax.set_xticks([])
  45. ax.set_yticks([])
  46. # Some advertisement for matplotlib
  47. year = time.strftime("%Y")
  48. text = ("The Mandelbrot fractal set\n"
  49. "Rendered with matplotlib %s, %s - http://matplotlib.org"
  50. % (matplotlib.__version__, year))
  51. ax.text(xmin+.025, ymin+.025, text, color="white", fontsize=12, alpha=0.5)
  52. plt.show()

Total running time of the script: ( 0 minutes 4.800 seconds)

下载这个示例