微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我试图在 python 中绘制 Mandelbrot 分形,但我没有得到所需的输出

如何解决我试图在 python 中绘制 Mandelbrot 分形,但我没有得到所需的输出

我是编码新手,我正在尝试创建一个基本的 Mandelbrot,但我的输出是一条直线。当我检查生成要绘制的两个列表的长度时,我的函数仅将 9 个元素放置在 Mandelbrot 集之外。有人可以看看我的代码并解释我做错了什么吗?我认为这与我不太了解 numpy 数组有关。

谢谢!

import numpy as np

import matplotlib.pyplot as plt

# We need to create an array containing all of the imaginary numbers contained within out grid.
x = np.linspace(-10,10,10000)

y = np.linspace(-10,10000)

x1,y1 = np.meshgrid(x,y) # Mesh both arrays together to get one of each point

c_array = x1 + (y1 * 1.0j)

# Empty lists that will hold our answers
black_list = []

red_list = []

# calculating using f_c (z) = z^2 + c
def perform_calculations(array,n):

    z = 0+0j

    for x_index in range(10000): # looping through every index in the array

        for y_index in range(10000):

            for iteration in range(n): # how many times the calculation will be performed

                c = c_array.item(x_index,y_index) # store every element in the array into c

                z = (z**2) + c # perform the calculation

            if abs(z) > 2:

                black_list.append(c) # place into the black list if it isn't a part of the set

            else:
                red_list.append(c) # place into the red list if it is a part of the set

        return black_list,red_list

# call the function
perform_calculations(c_array,51)

# separating the real and imaginary elements of the lists
black_real = [element.real for element in black_list] # place the real elements of the black list into a list

black_imaginary = [element.imag for element in black_list] # place the imaginary elements of the black list into a list

red_real = [element.real for element in red_list] # place the real elements of the red list into a list

red_imaginary = [element.imag for element in red_list] # place the imaginary elements of the red list into a list

# create the coordinate plane
plt.figure(figsize = (6,6))

plt.title('Practice Mandelbrot Set Image')

plt.xlim(-20,20)

plt.ylim(-20,20)

plt.xlabel('Real Components')

plt.ylabel('Imaginary Components')


# plot the list values in their respective colors
plt.scatter(black_real,black_imaginary,color = "k")

plt.scatter(red_real,red_imaginary,color = "red")

# show image
plt.show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。