Lewis Dean
Lewis Dean

Reputation: 31

How to centre an imshow() image?

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?

Upvotes: 1

Views: 371

Answers (1)

Amit Amola
Amit Amola

Reputation: 2510

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.

Upvotes: 1

Related Questions