MATLAB中的Python `imshow`

shyt4zoc  于 7个月前  发布在  Matlab
关注(0)|答案(1)|浏览(79)

如何在MATLAB中重新创建基本的plt.imshow(x)imshowimagesc不能处理任意浮点数据。
最低限度,vminvmax(颜色标准范围)也应该被复制。可选地,我希望能够使用一些MATLAB中没有的Python颜色Map表,特别是'bwr'-这可行吗?
下面是一个小例子。

import numpy as np
from numpy.fft import ifft, ifftshift
import matplotlib.pyplot as plt

# generate `x`
N, M = 2048, 512
wx, wy = np.linspace(0, 1, N, 0), np.linspace(0, 1, M, 0)
psi_x = ifftshift(ifft(np.exp(-(wx - .0125/2 )**2 * 2**19)))
psi_y = ifftshift(ifft(np.exp(-(wy - .00625*2)**2 * 2**16)))
x = psi_x * psi_y[:, None]

# make reusables
title_kw = dict(loc='left', weight='bold', fontsize=22)
mx = np.max(np.abs(x))

# plot
plt.imshow(x.real, cmap='bwr', vmin=-mx, vmax=mx, aspect='auto')
plt.title("Real part", **title_kw)
plt.gcf().set_size_inches((8, 8))
plt.show()

plt.imshow(abs(x), cmap='turbo', vmin=0, vmax=mx, aspect='auto')
plt.title('Absolute value', **title_kw)
plt.gcf().set_size_inches((8, 8))
plt.show()

vtwuwzda

vtwuwzda1#

下面的imshow完全重现了这个例子。
x缩放到[-255, 255]内并转换为int32使其适合imagesc。剩下的就是挖掘各种MATLAB文档的问题了:

  • vminvmax通过axis.CLim再现。
  • figsize是通过操作figure.Position来再现的。
  • set_size_inches-通过Python中的fig.dpi * figsize将Python的英寸转换为像素(我还没有完全测试过);还有figure.Units = 'inches',但这也会影响图形的位置
  • plt.titletitle复制
  • cmap是通过使用colormap来复制的。不支持的颜色Map表可以从Python中移植,如下面的示例所示。
  • set(ax, 'YDir', 'reverse')以匹配Python从上到下不断增加的垂直索引。imagesc在默认情况下并不总是这样做,即使没有指定'CData'(可能是一个bug,尽管下面的代码中没有;我在别处获取figax
  • 如果使用现有的figax(例如,子图),可能更适合执行imagesc(ax, 'CData', x),但请参阅高级与低级注解

示例重现

% arguments
norm = [];
figsize = [500 500];

% generate `x`
N = 2048;
M = 512;
wx = linspace(0, 1-1/N, N);
wy = linspace(0, 1-1/M, M);
psi_x = ifftshift(ifft(exp(-(wx - .0125/2 ).^2 * 2^19)));
psi_y = ifftshift(ifft(exp(-(wy - .00625*2).^2 * 2^16)));
x = (psi_x) .* (psi_y.');

% plot
imshow(real(x), norm, 'Real part',      0, 'bwr',   figsize)
imshow(x,       norm, 'Absolute value', 1, 'turbo', figsize)

移植色彩Map表

请参见this Q&A

imshow

function imshow(x, norm, title_txt, aval, cmap, figsize)    
    % handle input -------------------------------------------------------
    xa = abs(x);
    if aval
        x = xa;
    end
    % put the greater of minimum or maximum at 255
    scaling = 255 / max(xa, [], 'all');
    x = x * scaling;
    
    if ~isa(x, "int32")
        x = int32(x);
    end
    
    % basic plot ---------------------------------------------------------
    % generate canvas
    fig = figure;
    ax = gca;

    % `imshow`
    imagesc(ax, x);
    
    % styling meta -------------------------------------------------------
    % transform limits the same way
    if isempty(norm)
        if aval
            clims = [0 255];
        else
            clims = [-255 255];
        end
    else
        % matplotlib's `vmin`, `vmax` are specified w.r.t. `x`; 
        % below undoes this rescaling w.r.t. color norm
        clims = norm * scaling;
    end

    % styling ------------------------------------------------------------
    axis tight
    set(fig, 'color', 'white')  % white background
    set(ax, 'YDir', 'reverse')  % ensure y-axis increases from up to down
    
    % `title`
    if ~isempty(title_txt)
        title(title_txt)
        ax.TitleHorizontalAlignment = 'left';
    end
    
    % `cmap`
    try
        map = load([cmap, '.mat']).map;
    catch
        map = cmap;
    end
    colormap(fig, map)
    
    % `norm`
    ax.CLim = clims;
    
    % `figsize`
    if ~isnan(figsize)
        w = figsize(1);
        h = figsize(2);
        % get current [pos_x, pos_y, w, h]
        op = fig.Position;
    
        % set nans to current values
        if isnan(w)
            w = op(3);
        end
        if isnan(h)
            h = op(4);
        end
    
        % reuse old pos_x, pos_y (may not be optimal)
        fig.Position = [op(1:2), w, h];
    end
end

什么是情节?2D separable Morlet wavelet

相关问题