使用 matplotlib.pyplot 和 numpy 显示 python 中的 Mandelbrot 集

displaying Mandelbrot set in python using matplotlib.pyplot and numpy

我正在尝试绘制 Mandelbrot 集的图,但在绘制预期图时遇到了问题。

据我了解,Mandelbrot 集由值 c 组成,如果通过以下等式 z = z**2 + c 进行迭代,它将收敛。我使用了 z = 0 的初始值。

最初,我得到的是一条直线。我在网上寻找解决方案,看看我哪里出错了。特别是使用以下 link,我试图改进我的代码:

https://scipy-lectures.org/intro/numpy/auto_examples/plot_mandelbrot.html

这是我改进后的代码。我真的不明白使用 np.newaxis 的原因以及为什么我要绘制收敛的最终 z 值。我误解了 Mandelbrot 集的定义吗?

# initial values 
loop = 50 # number of interations
div = 600 # divisions
# all possible values of c
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:] 
z = 0 
for n in range(0,loop):
      z = z**2 + c

plt.rcParams['figure.figsize'] = [12, 7.5]
z = z[abs(z) < 2] # removing z values that diverge 
plt.scatter(z.real, z.imag, color = "black" ) # plotting points
plt.xlabel("Real")
plt.ylabel("i (imaginary)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.savefig("plot.png")
plt.show()

并得到了下面的图像,它看起来比我目前得到的任何图像都更接近 Mandelbrot 集。但它看起来更像是一个周围散布着圆点的海星。 Image

作为参考,这是我改进前的初始代码:

# initial values 
loop = 50
div = 50
clist = np.linspace(-2,2,div) + 1j*np.linspace(-1.5,1.5,div) # range of c values 
all_results = []

for c in clist: # for each value of c
    z = 0 # starting point
    for a in range(0,loop): 
        negative = 0 # unstable

        z = z**2 + c 

        if np.abs(z) > 2: 
            negative +=1
        if negative > 2: 
            break

    if negative == 0:
        all_results.append([c,"blue"]) #converging
    else:
        all_results.append([c,"black"]) # not converging

绘图看起来不正确,因为在问题的代码中绘制了 z(即迭代变量)。迭代 z = z*z + c,Mandelbrot 集由 c 的那些实部、虚部对给出,对于它们,级数不发散。因此,如下所示对代码的小改动给出了正确的 Mandelbrot 图:

import pylab as plt
import numpy as np
# initial values 
loop = 50 # number of interations
div = 600 # divisions
# all possible values of c
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:] 
z = 0 
for n in range(0,loop):
      z = z**2 + c

plt.rcParams['figure.figsize'] = [12, 7.5]
p = c[abs(z) < 2] # removing c values for which z has diverged 
plt.scatter(p.real, p.imag, color = "black" ) # plotting points
plt.xlabel("Real")
plt.ylabel("i (imaginary)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.savefig("plot.png")
plt.show()

或者,通过对问题中的代码进行另一个小的更改,可以使用 z 的值对图进行着色。可以存储 n 的值,其中系列的绝对值变得大于 2(意味着它发散),并用它为 Mandelbrot 集之外的点着色:

import pylab as plt
import numpy as np
# initial values 
loop = 50 # number of interations
div = 600 # divisions
# all possible values of c
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:] 
# array of ones of same dimensions as c
ones = np.ones(np.shape(c), np.int)
# Array that will hold colors for plot, initial value set here will be
# the color of the points in the mandelbrot set, i.e. where the series
# converges.
# For the code below to work, this initial value must at least be 'loop'.
# Here it is loop + 5
color = ones * loop + 5
z = 0
for n in range(0,loop):
      z = z**2 + c
      diverged = np.abs(z)>2
      # Store value of n at which series was detected to diverge.
      # The later the series is detected to diverge, the higher
      # the 'color' value.
      color[diverged] = np.minimum(color[diverged], ones[diverged]*n)

plt.rcParams['figure.figsize'] = [12, 7.5]
# contour plot with real and imaginary parts of c as axes
# and colored according to 'color'
plt.contourf(c.real, c.imag, color)
plt.xlabel("Real($c$)")
plt.ylabel("Imag($c$)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.savefig("plot.png")
plt.show()