# Thread: Python implementation of the mandelbrot fractal set

1. ## Python implementation of the mandelbrot fractal set

Hi guys,

I appreciate your help in the past, it was valuable this time I need a little help in order to understand this mandelbrot fractal set.

To begin with what is a fractal? Since English is not my native language, I have a hard time translating this.

This is my code, I want to know how to generate the image and how to calculate this fractal. I got to how many iterations are needed for (Z on) 2) + Z + C, iterations of what?

Code:
```#Description:
#A mandelbrot set uses a decart coordinate system in order to represent an image and its color based on the
#iterations needed in order to

#Libraries
#From pillow(PIL) include the classes Image and ImageDraw
from PIL import Image, ImageDraw
#Import libraries os, json, yaml, math
import os, json, yaml, math
#Import library pandas named pd for short
import pandas as pd
#Import library numbpy named np for short
import numpy as np
#Import library matplotlib . pylot named plt for short
import matplotlib .pyplot as plt

#Variables block
image_width = 100
image_height = 100
cxmin = 100
cxmax = 100
cymin = 100
cymax = 100
iterations = 100
color = 'Green'
output_image_path = 'target/04_mandelbrot.png'

#Function declarations block
#mandelbrot set function
def render_mandelbrot(image_width, image_height, cxmin, cxmax, cymin, cymax, iterations, color, output_image_path):
#create an image of type RGBA, size in width and height, color
img = Image.new('RGBA', (image_width, image_height), color)

#Mandelbrot calculations(scaling)
for i in range (image_height):
y = 2*i/image_height - 1.0
for j in range (image_width):
x = 2*j/image_width - 1.0
#compute the mandelbrot set iteself
g = compute_mandelbrot_value(x, y, iterations=200)

#color = (g, g, g)
#new_pixel_map[j, i] = color
#return the image at the end of the funtion
return img

def compute_mandelbrot_value(x, y, iterations=200):
g = int(255 * ((x + y)+ 2)/4)
return g
img = Image.new('RGB', (image_width, image_height), color)
img.save(output_image_path)#The function does not save the file

#Function calling block
render_mandelbrot(image_width, image_height, cxmin, cxmax, cymin, cymax, iterations, color, output_image_path)```

2. Latest code for anyone who needs it, its not perfect and there might be mistakes in the comments:
Code:
```# Description:
# To iterate (z on the power of 2) + c we begin with a seed for the iteration. This is a real or complex number
# which we denote by z0. Applying the function (z on the power of 2) + c on z0, gives the new number.
# Now we iterate using the result of the previous computation as the input for the next.
# z0 = 0(this is the seed)
# z1 = (z0 on the power of 2) + c
# z2 = (z1 on the power of 2) + c
# and so on.
# if we take 0 for the seed, z0 = 0
# z1 = (0 on the power of 2) + 1 = 1
# z2 = (1 on the power of 2) + 1 = 2
# and so on
import numpy as np
import matplotlib.pyplot as plt
from matplotlib .pyplot import imshow
from numpy import newaxis
import cmath # importing cmat for complex number operations

def compute_mandelbrot(N_max, some_threshold, nx, ny):      # N_max = max iterations
# some threshold = threshold for z after which to stop
# nx = image height
# ny = image width

# A grid of c-values
x = np.linspace(-2, 1, nx)                              # numpy linspace returns evenly placed numbers over a
# specified interval
y = np.linspace(-1.5, 1.5, ny)                          # y = np.linspace(interval start, interval end, points)
# y equals an array of numbers between -1.5 and 1.5,
# with ny points, in this case 401, the numbers are
# evenly placed

c = x[:,newaxis] + 1j*y[newaxis,:]                      # All values of c, these are the points on the image

# Mandelbrot iteration
z = c
# The code below overflows in many regions of the x-y grid, suppress
# warnings temporarily
with np.warnings.catch_warnings():                      # We start the iterations by the formula
np.warnings.simplefilter("ignore")                  # z(p) = (z(p-1) on the power of 2) + C
for j in range(N_max):
z = z**2 + c                                    # The mandelbrot set is the formula z(1) = (z0 on the power of 2) + c
mandelbrot_set = (abs(z) < some_threshold)          # After we calculate z, we get the absolute value
# If we give abs() a complex number, then the function
# returns the magnitude of that number
print (z)                                           # Prints nan + nan
print (mandelbrot_set)                              # Prints false
return mandelbrot_set

mandelbrot_set = compute_mandelbrot(50, 50., 601, 401)     # Max iterations, threshold, image height, image width

plt.imshow(mandelbrot_set.T, extent=[-2, 1, -1.5, 1.5])    # The image has dimensions nx and ny, but we scale it to -2, 1 for x and -1.5, 1.5 for y
plt.gray()                                                 # We assign the right color to our mandelbrot set
plt.show()                                                 # We show the image

imshow(np.asarray(mandelbrot_set))```