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

我正在尝试绘制Mandelbrot集的图,并且在绘制预期图时遇到了麻烦。

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

最初,我获得了一条直线。我在网上寻找解决方案,以查看哪里出了问题。特别是使用以下链接,我试图改进代码:

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的值为绘图着色。可以将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()

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

如何在Kivy中开始/使用Matplotlib - python

我最近学到了一些matplotlib,想在kivy中使用它。我已经在这里和那里的花园里阅读了一些文档,但并不太了解。我已经安装了kivy garden和matplotlib,但是不知道如何从这里继续。我只是想将一个已经完成的matplotlib图添加到kivy中。我将很欣赏逐步简化的指令集,该指令集如何获取已经编码为kivy的内容并显示出来。谢谢 参考方案 …

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python:无法识别Pip命令 - python

这是我拍摄的屏幕截图。当我尝试在命令提示符下使用pip时,出现以下错误消息:pip无法识别为内部或外部命令,可操作程序或批处理文件。我已经检查了这个线程:How do I install pip on Windows?我所能找到的就是我必须将"C:\PythonX\Scripts"添加到我的类路径中,其中X代表python版本。如您在我的…

Python:如何将有效的uuid从String转换为UUID? - python

我收到的数据是 { "name": "Unknown", "parent": "Uncategorized", "uuid": "06335e84-2872-4914-8c5d-3ed07d2a2f16" }, 我需要将uuid从Strin…

Python 3会流行吗? - python

我已经学习了一些Python 2和Python 3,似乎Python 2总体上比Python 3更好。这就是我的问题所在。是否有充分的理由真正切换到python 3? 参考方案 总体上,甚至在大多数细节上,Python3都比Python2更好。关于第三方库, Python 3落后于的唯一区域是。使Python变得如此出色的原因不仅在于它作为一种语言的内在特性…