如何将 imshow() 图像居中?

How to centre an imshow() image?

我有一个项目,我需要在其中使用 python 创建 mandelbrot 和 julia 集。我有两组不同的函数 'work'。第一个不完全符合我的标准,因为它没有采用一组值。第二个做了我需要的一切,但图像没有居中显示。

这是第二个代码:

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)

图片显示:non-centred version 但我需要它像这样居中:centred version

所以问题是:如何使用上面添加的代码将图像居中?

好吧,你写的没有任何问题,事实上,你在这里得到了多么漂亮的结果。看起来很像 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)

顺便说一句,非常令人印象深刻的作品。