我有以下代码:
# MANDELBROT
a = np.arange(-2, 2, 0.01)
b = np.arange(-2, 2, 0.01)
M_new = new_matrix(a, b)
plt.imshow(M_new, cmap='gray', extent=(-2, 2, -2, 2))
plt.show()
## ZOOMING
a_2 = np.arange(0.1, 0.5, 0.01)
b_2 = np.arange(0.1, 0.5, 0.01)
M_new_2 = new_matrix(a_2, b_2)
plt.imshow(M_new_2, cmap='gray', extent=(0.1, 0.5, 0.1, 0.5))
plt.show()当我绘制Mandelbrot时,它看起来很好,但是接下来我要做的是放大集合的特定部分( 0,1 - 0,5,x和y-轴之间)。我不知道代码的第二部分是否不正确,或者我想错了。
发布于 2021-06-10 14:13:28
我对代码做了几处更改,主要是关于输入数组的形状。
import numpy as np
from matplotlib import pyplot as plt
def mandelbrot(c: complex):
m = 0
z = complex(0, 0)
for i in range(0, 100):
z = z*z+c
m += 1
if np.abs(z) > 2:
return False
return True
def new_matrix(a_1, b_1):
M = np.zeros((a_1.shape[0], b_1.shape[0]))
for i, number1 in enumerate(a_1):
for j, number2 in enumerate(b_1):
M[i, j] = mandelbrot(complex(number1, number2))
return M
# MANDELBROT
a = np.arange(-2, 2, 0.01)
b = np.arange(-2, 2, 0.01)
M_new = new_matrix(a, b)
plt.imshow(M_new, cmap='gray', extent=(-2, 2, -2, 2))
plt.show()
## ZOOMING
a_2 = np.arange(0.1, 0.5, 0.01)
b_2 = np.arange(0.1, 0.5, 0.01)
M_new_2 = new_matrix(a_2, b_2)
plt.imshow(M_new_2, cmap='gray', extent=(0.1, 0.5, 0.1, 0.5))
plt.show()特别检查矩阵M的定义(使用输入参数的形状,以及for循环中的枚举结构)。
毫无疑问,它可以被进一步优化,但至少这会使它发挥作用。您可能希望在应用程序中使用linspace,以便更好地控制生成的点数。
发布于 2021-06-10 14:36:27
确保生成所需的数组大小(401x401),并在矩阵中正确地绘制这些点。当您放大时,您不希望在区段之间进行.01增量,而只需要401个步骤。为此请使用np.linspace。此外,创建一个显示矩阵的通用函数有助于使数学一致:
import numpy as np
from matplotlib import pyplot as plt
SIZE = 401
def mandelbrot(c: complex):
z = complex(0, 0)
for i in range(100):
z = z*z+c
return np.abs(z) <= 2
def new_matrix(a_1, b_1):
M = np.zeros(SIZE*SIZE).reshape(SIZE, SIZE)
for i,number1 in enumerate(a_1):
for j,number2 in enumerate(b_1):
M[i,j] = mandelbrot(complex(number1, number2))
return M
def show(x1,y1,x2,y2):
a = np.linspace(x1, y1, SIZE)
b = np.linspace(x2, y2, SIZE)
M = new_matrix(a, b)
plt.imshow(M, cmap='gray', extent=(x1, y1, x2, y2))
plt.show()
show(-2,2,-2,2)
show(.1,.5,.1,.5)输出:


https://stackoverflow.com/questions/67922408
复制相似问题