本文介绍了如何将原点居中在 imshow() 图的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个长程序的一部分,该程序可以模拟通过孔径的衍射,因此我不禁尝试使所得的 plt.imshow()的原点位于图的中心,即我想改变轴.

As a part of a long program to simulate diffraction through an aperture, I'm fiddling around trying to get my resulting plt.imshow() to have its origin in the centre of the plot, i.e. I wish to change the axes.

相关代码部分为:

    n=40
    lam=0.0006
    k=(2*np.pi)/lam
    z=float(input("Type screen distance, z in mm: "))
    rho=float(input("Type rho in mm: "))

    delta = 2/n
    intensity = np.zeros((n+1,n+1))

    for i in range(n+1):
        x=-1+(i*delta)
        for j in range(n+1):
            y =-1+(j*delta)
            intensity[i,j] = (abs(square_2dsimpson_eval(-rho/2,rho/2,n)))**2
    plt.imshow(intensity)
    plt.show()

生成下面的图.提前致谢.

generating the below plot. Thanks in advance.

推荐答案

为了将原点放置在图像绘图的中心,可以使用对称的 extent .

In order to position the origin at the center of the image plot, you can use a symmetric extent.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1,2,1,4],[2,5,3,1],[0,1,2,2]])
plt.imshow(x, extent=[-x.shape[1]/2., x.shape[1]/2., -x.shape[0]/2., x.shape[0]/2. ])
plt.show()

这篇关于如何将原点居中在 imshow() 图的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:28