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?