简体   繁体   English

如何在 Python3 imshow() 的 MatLab 中的 imshow() 中使用“xdata”

[英]How to use 'xdata' in imshow() from MatLab in Python3 imshow()

I'm trying to convert some MatLab code to Python3 but am having trouble with one line, the line in MatLab is我正在尝试将一些 MatLab 代码转换为 Python3,但在一行中遇到问题,MatLab 中的这一行是

imshow(sqrt(I),[0,100],'InitialMagnification','fit','xdata',[-1,1]*a,'ydata',[-1,1]*a);

Where a is some constant.其中a是某个常数。

The problems I'm having are with 'XData' and 'YData' .我遇到的问题是'XData''YData' I'm currently using the Python code我目前正在使用 Python 代码

matplotlib.pyplot.imshow(np.sqrt(np.real(I)), vmin = 0, vmax = 100)

How do I convert the 'XData' and 'YData' elements into Python?如何将'XData''YData'元素转换为 Python?

EDIT Full Code for Python and MatLab编辑 Python 和 MatLab 的完整代码

Python code:蟒蛇代码:

(I'm sooo sorry, but I haven't commented this code yet, I can do so if needed) (我很抱歉,但我还没有评论这段代码,如果需要我可以这样做)

import numpy as np
import matplotlib.pyplot as plt

plt.close("all")

l = 633*10**(-6)
L = 10

N = 2**11
Nx = 2**11
Ny = 2**11

xmax = 5*10**(-4)
ymax = 5*10**(-4)
curlyv = np.zeros((Nx,Ny),dtype=np.double)
x = np.tile(np.linspace(-xmax,xmax,Nx),(Ny,1))
y = np.tile(np.linspace(-ymax,ymax,Ny).reshape(Ny,1),(1,Nx))
h = np.sqrt(4*np.pi/(l*L))

np_x = np.array(x)
np_y = np.array(y)

aperture = np.sqrt(np_x**2 + np_y**2) >= 100*10**-6

curlyv[aperture] = 1    

vprime = np.fft.fftshift(np.fft.fft2(curlyv))
I = vprime*np.conj(vprime)
Imax = np.real(np.amax(I))
fig2 = plt.figure()
Imax_b = Imax/5000
pltp = plt.imshow(np.sqrt(np.real(I)), vmin = 0, vmax = 100, cmap='Greys_r', extent=(-Nx/h*1000, Nx/h*1000, -Ny/h*1000, Ny/h*1000))

plt.show()

Python image:蟒蛇图像:

蟒蛇图像

MatLab Code MatLab 代码

% This script calculates, via a 2D Fourier Transform, the Fraunhofer diffraction
% pattern due to a circular aperture. Source plane is the xy-plane. The field plane
% is at a distance L from the source plane.

lambda = 633e-6; L = 10;                     % meters

% Set up the source plane domain and initialize the source plane amplitude (curlyv) at each point
N=2^11; Nx = N; Ny = N;                         % resolution (pixels)
xmax=5e-4;                                  % meters
ymax=5e-4;                                  % meters
curlyv = zeros(Nx,Ny);                      % curlyv has one complex value at each location (x,y)
x=repmat(linspace(-xmax,xmax,Nx),Ny,1);     % x domain (source plane)
y=repmat(linspace(-ymax,ymax,Ny)',1,Nx);    % y domain (source plane)
h = sqrt(4*pi/(lambda*L));                  % axes scaling (from the theory)

% construct a logical vector corresponding to the (x,y)-locations where the incident field is
% non-zero. Only need to set these elements of curlyv so some non-zero values
% corresponding to the incident field's complex amplitude.

aperture = sqrt(x.^2+y.^2) >= 100e-6;       % logical vector giving aperture shape
% The incident field ampli0tude u(x,y) is just 1 everywhere in the aperture. The additional
% exponential corresponding to the phase in curlyv is formally required from the theory but can be omitted to a good
% approximation in most cases.
curlyv(aperture)=1;                         %exp(1i*h^2*(x(aperture).^2+y(aperture).^2));

figure(1);
iptsetpref('ImShowAxesVisible','On');
imshow(curlyv,[-0.5 1],'InitialMagnification','fit',...
    'xdata',[-xmax, xmax]*1000,'ydata',[-ymax,ymax]*1000); % display the diffraction pattern
vprime = fftshift(fft2(curlyv,Nx,Nx));      % perform the 2D FT to get the field plane field amplitude
I = conj(vprime).*vprime;                   % calculate intensity
xlabel('mm'); ylabel('mm');

figure(2);
Imax=max(max(I));
iptsetpref('ImShowAxesVisible','On');
imshow(sqrt(I),[0,sqrt(Imax)/50],'InitialMagnification','fit','xdata',[-1,1]*Nx/h*1000,'ydata',[-1,1]*Ny/h*1000); % display the diffraction pattern

colormap(bone); shg;  shading interp;
axis([-2,2,-2,2]*1000);       % 0 =  black, Imax/10 = white (so many pixels will be saturated)
xlabel('mm'); ylabel('mm');

MatLab Image (what the images should look like) MatLab 图像(图像应该是什么样子)

在此处输入图片说明

XData for imshow in MATLAB sets the MATLAB 中imshow XData设置

Limits along X axis of a nondefault spatial coordinate system, specified as a two-element vector沿非默认空间坐标系的 X 轴的限制,指定为二元素向量

(similar for YData and the y axis). (类似于YData和 y 轴)。 For matplotlib.pyplot.imshow , the extent parameter seems to have the same effect:对于matplotlib.pyplot.imshowextent参数似乎具有相同的效果:

extent : scalars (left, right, bottom, top), optional, default: None范围:标量(左、右、下、上),可选,默认值: None

The location, in data-coordinates, of the lower-left and upper-right corners.数据坐标中左下角和右上角的位置。 If None , the image is positioned such that the pixel centers fall on zero-based (row, column) indices.如果None ,图像的位置使得像素中心落在基于零的(行、列)索引上。

Hence the equivalent call would be因此等效的调用将是

matplotlib.pyplot.imshow(np.sqrt(np.real(I)), vmin = 0, vmax = 100, extent=(-a, a, -a, a))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM