首页 > 解决方案 > 我不知道为什么相同的代码适用于 Julia 而不适用于 Mandelbrot?

问题描述

我有以下代码生成 Mandelbrot 图像。图像周围的空白区域,必须消除。

import numpy as np
import matplotlib.pyplot as plt
from pylab import *
from numpy import NaN

def mandelbrot(C):
    z = 0
    for n in range(1, 10):
        z = z**2 + C
        if abs(z) > 2:
            return n
    return NaN

def plot():
    X = np.arange(-2.0, 1.0, 0.05)
    Y = np.arange(-1.5, 1.5, 0.05)
    pixel = np.zeros((len(Y), len(X)))

    for x_iter, x in enumerate(X):
        for y_iter, y in enumerate(Y):
            pixel[y_iter, x_iter] = mandelbrot(x + 1j * y)

        imshow(pixel, cmap = 'gray', extent = (X.min(), X.max(), Y.min(), Y.max()))

    return pixel

pixel = mandelbrot(-0.7 + 0.27015j)
plt.axis('off')  
plot()
plt.show()

from PIL import Image
min_value = np.nanmin(pixel)
max_value = np.nanmax(pixel)
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)
# sample LUT from matplotlib
lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8) # CHOOSE COLORMAP HERE viridis, jet, rainbow
pixel_rgb = lut[pixel_int]
# changing NaNs to a chosen color
nan_color = [0,0,0,0] # Transparent NaNs
for i,c in enumerate(nan_color):
  pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])
# apply LUT and display
img = Image.fromarray(pixel_rgb, 'RGBA')

print(pixel)

但事实证明 IndexError: too many indices for array for the line

pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])

请问,怎么修?

实际上,为了消除图像周围的空白,几周前相同的代码(同一行)已经为 Julia 而不是 Mandelbrot 工作了。以下生成 Julia 图像的代码正在消除图像周围的空白。

import numpy as np
import matplotlib.pyplot as plt

def julia(C):
    X = np.arange(-1.5, 1.5, 0.05)
    Y = np.arange(-1.5, 1.5, 0.05)
    pixel = np.zeros((len(Y), len(X)))

    for x_iter, x in enumerate(X):
        for y_iter, y in enumerate(Y):
            z = x + 1j * y
            intensity = np.nan
            r = np.empty((100, 100)) # Unused at the moment
            for n in range(1, 1024):
                if abs(z) > 2:
                    intensity = n
                    break
                z = z**2 + C
            pixel[y_iter, x_iter] = intensity
            r.fill(intensity) # Unused at the moment

    # We return pixel matrix
    return pixel

# Compute Julia set image
pixel = julia(-0.7 + 0.27015j)

# Plotting
print(pixel)
plt.show()

from PIL import Image
min_value = np.nanmin(pixel)
max_value = np.nanmax(pixel)  
#want to set all the 255 pixels to removed
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)
# sample LUT from matplotlib,If lut is not None it must be an integer giving the number of entries desired in the lookup table
lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8) # CHOOSE COLORMAP HERE viridis, jet, rainbow
pixel_rgb = lut[pixel_int]
# changing NaNs to a chosen color
nan_color = [0,0,0,0] # Transparent NaNs
for i,c in enumerate(nan_color):
  pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])
# apply LUT and display
img = Image.fromarray(pixel_rgb, 'RGBA')
img.save('julia.tiff')
Image.open('julia.tiff').show()
print(min_value, max_value)

现在,我只是不知道为什么这个摆脱图像周围空白的代码对 Mandelbrot 不起作用?!请帮我找出问题所在!

标签: pythonfractalsmandelbrot

解决方案


您的直接问题是,在 Julia 案例中,pixel_rgb是一个三维数组,而在 Mandelbrot 案例中,pixel_rgb是一个一维数组。所以你试图对它们中的每一个应用一个三维变换,这对于 Mandelbrot 案例来说很糟糕,因为你正在操作的只有一个维度,而不是三个。

我没有更多时间来完全理解和使用您的代码,但在 Mandelbrot 的情况下,该mandelbrot()函数似乎只返回一个值,该julia()函数返回一个二维数组。它是plot()在 Mandelbrot 案例中返回二维数组的函数。所以我对你想要做的改变的快速猜测是改变这个:

pixel = mandelbrot(-0.7 + 0.27015j)
plt.axis('off')
plot()

对此:

# pixel = mandelbrot(-0.7 + 0.27015j)
plt.axis('off')
pixel = plot()

这允许 Mandelbrot 代码运行而不会崩溃。我不知道它是否正在做你想要的。


推荐阅读