1

I have a project in which I need to create mandelbrot and julia sets using python. I have two different sets of functions that 'work'. The first doesn't fully meet my criteria because it isn't taking sets of values. The second does everything I need, but the image is not shown centred.

This is the second code:

def julia(xvals, yvals, c, Threshold):
    max_iteration=50
    z=complex(xvals,yvals)

    for i in range(max_iteration):
        z = z*z + c
        if (z.real*z.real + z.imag*z.imag)>=Threshold*Threshold:
            return i
    return max_iteration

def Julia(xvals,yvals,c,Threshold):
    '''Input:
    xvals; numpy list containing x co-ordinates, 
    yvals; numpy list containing y co-ordinates, 
    c; a complex number, in the form of [x + yj] or complex(x, y), where x and y are numbers,
    Threshold; a positive number, recommended 2,
    Output: Julia set plot and True if successful,
    Produces the plot for the respective Julia set for complex number c, iterated for |z|>Threshold, and returns True if successful.'''
    # preliminary tests
    assert isinstance(xvals,np.ndarray), 'xvals must be a real, positive, number.'
    assert isinstance(yvals,np.ndarray), 'yvals must be a real, positive, number.'
    assert isinstance(Threshold,(int, float)), 'Threshold must be a real, positive, number.'
    assert Threshold>0, 'Threshold must be more than 0.'
    # iteration
    columns = len(yvals)
    rows = len(xvals)
    result = np.zeros([rows, columns])
    for row_index, xvals in enumerate(np.linspace(-2, 1, num=rows)):
        for column_index, yvals in enumerate(np.linspace(-1.5, 1.5, num=columns)):
            result[row_index, column_index] = julia(xvals, yvals, c, Threshold)
    # plot
    fig, ax = plt.subplots()
    ax.imshow(result.T, extent=[-1.5, 1.5, -1.5, 1.5], interpolation='bilinear', cmap='hot')
    plt.xlabel('Real Numbers')
    plt.ylabel('Imaginary Numbers')
    plt.title("Julia Set for " + str(c))
    plt.tight_layout
    plt.show()
    return True

x = np.linspace(-1.5, 1.5, 601)
y = np.linspace(-1.5, 1.5, 401)
Julia(x, y, complex(-0.7269, 0.1889), 4)

The image shows: non-centred version But I need it to be centred like this: centred version

So the question is: how do I centre the image with the code I have added above?

Amit Amola
  • 2,301
  • 2
  • 22
  • 37
Lewis Dean
  • 31
  • 1
  • 3

1 Answers1

1

Well there is nothing wrong with what you've written and in fact, what a beautiful result you've got here. Looks so much like the Mandelbrot set.

Now here's the reason why you were getting that image as output. The only thing you need to change is the np.linspace range that you are giving.

>>> for row_index, xvals in enumerate(np.linspace(-1.6, 1.6, num=rows)):

That's all and it will give you this: enter image description here

I personally made lot of other changes to the code(visual only) and made it like this. Hope you like this one better.

def julia(xvals, yvals, c, Threshold):
    max_iteration=50
    z=complex(xvals,yvals)

    for i in range(max_iteration):
        z = z*z + c
        if (z.real*z.real + z.imag*z.imag)>=Threshold*Threshold:
            return i
    return max_iteration

def Julia(xvals,yvals,c,Threshold):
    '''Input:
    xvals; numpy list containing x co-ordinates, 
    yvals; numpy list containing y co-ordinates, 
    c; a complex number, in the form of [x + yj] or complex(x, y), where x and y are numbers,
    Threshold; a positive number, recommended 2,
    Output: Julia set plot and True if successful,
    Produces the plot for the respective Julia set for complex number c, iterated for |z|>Threshold, and returns True if successful.'''
    # preliminary tests
    assert isinstance(xvals,np.ndarray), 'xvals must be a real, positive, number.'
    assert isinstance(yvals,np.ndarray), 'yvals must be a real, positive, number.'
    assert isinstance(Threshold,(int, float)), 'Threshold must be a real, positive, number.'
    assert Threshold>0, 'Threshold must be more than 0.'
    # iteration
    columns = len(yvals)
    rows = len(xvals)
    result = np.zeros([rows, columns])
    for row_index, xvals in enumerate(np.linspace(-1.6, 1.6, num=rows)):
        for column_index, yvals in enumerate(np.linspace(-1.5, 1.5, num=columns)):
            result[row_index, column_index] = julia(xvals, yvals, c, Threshold)
    # plot
    plt.figure(figsize=(7,12))
    plt.imshow(result.T, extent=[-1.5, 1.5, -1.5, 1.5], interpolation='bilinear', cmap='hot')
    plt.xlabel('Real Numbers')
    plt.ylabel('Imaginary Numbers')
    plt.title("Julia Set for " + str(c))
    plt.tight_layout()
    plt.grid(b=False)
    plt.show()
    return result.T

x = np.linspace(-1.5, 1.5, 601)
y = np.linspace(-1.5, 1.5, 401)
k = Julia(x, y, complex(-0.7269, 0.1889), 4)

enter image description here

Pretty impressive work by the way.

Amit Amola
  • 2,301
  • 2
  • 22
  • 37
  • 1
    This is fantastic. Thank you very much, I have the code for a MandelBrot if you were interested. – Lewis Dean Dec 15 '20 at 19:57
  • This code above is that right? Or you've something else too? I am just aware of ManderBrot sets, but never got time to read about them or even plot them on Python. I'd love to connect with you more on this. – Amit Amola Dec 16 '20 at 06:24
  • Of course, these are just Julia sets, using specific values of c, while the Mandelbrot set (from my understanding) uses z=0 and iterates from c, and is the representation of the set of which julias are available. – Lewis Dean Dec 16 '20 at 19:22
  • 1
    To confirm, I have completed mandelbrot (it's essentially the same by remove c from everything and start with z=0.) So I just wondered if you were interested I could supply you these. – Lewis Dean Dec 16 '20 at 20:18