python - 如何使 imshow() 图像居中?
问题描述
我有一个项目,我需要使用 python 创建 mandelbrot 和 julia 集。我有两组不同的“工作”功能。第一个不完全符合我的标准,因为它没有采用一组值。第二个做了我需要的一切,但图像没有居中显示。
这是第二个代码:
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)
所以问题是:如何将图像与上面添加的代码居中?
解决方案
好吧,您所写的内容没有任何问题,事实上,您在这里得到了多么美好的结果。看起来很像 Mandelbrot 套装。
现在这就是您将该图像作为输出的原因。您唯一需要更改的是您提供的 np.linspace 范围。
>>> for row_index, xvals in enumerate(np.linspace(-1.6, 1.6, num=rows)):
我个人对代码进行了许多其他更改(仅限视觉)并使其成为这样。希望你更喜欢这个。
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)
顺便说一句,相当令人印象深刻的工作。
推荐阅读
- java - 在刷新刷新之前清除列表视图
- spring-webflux - 基本单声道
- spotfire - Spotfire:是否有(内置)函数来测试一行是否被标记?
- javascript - 简单的 React 计数器不起作用,console.log 也不能使用状态变量?
- java - Java ProcessBuilder + bash:“没有这样的文件或目录”
- c++ - C++:如何在一个线程中读取二进制文件并在另一个线程中写入?
- java - 如何使 TextView 的最大行数 - 如果更多 - 可滚动
- ansible - 使用 --step 选项运行 ansible-playbook 时重新启动失败的任务
- paypal - Paypal-credit 智能支付按钮未显示
- java - 我可以在具有多个实例的类上使用 Random 方法吗?