【问题标题】:How to centre an imshow() image?如何使 imshow() 图像居中?
【发布时间】:2021-03-25 16:05:54
【问题描述】:

我有一个项目,我需要在其中使用 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)

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

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

【问题讨论】:

    标签: python matplotlib imshow mandelbrot


    【解决方案1】:

    好吧,您所写的内容并没有错,事实上,您在这里得到了多么美好的结果。看起来很像 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)
    

    顺便说一句,相当令人印象深刻的工作。

    【讨论】:

    • 这太棒了。非常感谢,如果您有兴趣,我有 MandelBrot 的代码。
    • 上面这段代码对吗?还是你也有别的东西?我只知道 ManderBrot 集,但从来没有时间阅读它们,甚至没有时间在 Python 上绘制它们。我很想在这方面与您有更多联系。
    • 当然,这些只是 Julia 集,使用 c 的特定值,而 Mandelbrot 集(根据我的理解)使用 z=0 并从 c 迭代,并且是其集合的表示朱莉娅可用。
    • 确认一下,我已经完成了 mandelbrot(从所有内容中删除 c 并从 z=0 开始基本上是相同的。)所以我只是想知道您是否有兴趣我可以为您提供这些。
    猜你喜欢
    • 2014-02-28
    • 2016-04-23
    • 2020-01-29
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多