【问题标题】:Multiple Plot Matlab including image多图 Matlab 包括图像
【发布时间】:2021-01-29 13:02:44
【问题描述】:

我使用tiledlayout(2,2) 命令在 Matlab 中创建绘图。前三个标题字段填充了正常的地块。第四个也是最后一个字段是带有图像的字段。我使用了命令IMG = 'mypic.tif'imshow(IMG)。我想知道为什么我不能更改图像的大小。 tiledlayoutcommand 无法做到这一点吗?我查找了tiledlayoutimshow() 的纪录片,但没有找到任何对我有帮助的。

【问题讨论】:

  • 你试过InnerPositionproperty吗?
  • 我今天试了一下,但它使三个地块和图像变大或变小。我只是想让图像更大,并且关闭的地块保持相同的大小。

标签: matlab matlab-figure


【解决方案1】:

使用tiledlayout 功能似乎确实无法做到这一点。但是,可以使用subplot 功能。下面,我将举例说明这是如何工作的。

考虑以下脚本:

% Generate some dummy data for the four plots
x = linspace(0, 2*pi, 25);
y = sin(x);

% Load sample image
image = imread('ngc6543a.jpg');

% Generate the figure
fig = figure(1);

% The three subplots
for i = 1:3
    subplot(2,2,i);
    plot(x, y);
end

% The image
subplot(2,2,4);
imshow(image);

% Increase size of the image
size_factor = 1.4; % 1.0 is original size

im_ax = fig.Children(1);    % axes belonging to image
dims = im_ax.Position(3:4); % current dimensions

dims_new = size_factor * dims;  % scale old dimensions
dxdy = (dims_new - dims) / 2;   % offset for left bottom corner of image

im_ax.Position = [im_ax.Position(1:2) - dxdy, dims_new]; % update position

在此脚本中,我们首先为三个“正常”图生成一些虚拟数据并加载示例图像(此图像随我的MATLAB 安装提供)。随后,我们创建一个图形。创建图后,我使用for 循环和subplot 功能将三个虚拟图添加到图中。然后,我们在第四个子图中使用imshow 绘制图像。

现在,有趣的部分开始了。首先,我为图像定义了一个比例因子(size_factor)。然后,我检索绘制图像的轴并将其存储在im_ax 中。从这个坐标轴中,我检索Position 字段的最后两个元素并将它们存储在dims 中。这两个元素定义了轴的大小。根据size_factor 的值,我计算新的坐标轴大小并将其存储在dims_new 中。为了确保坐标轴(以及图像)保持居中,我们需要计算坐标轴左下角需要移动多少(坐标存储在Position 字段的前两个元素中)。此结果存储在dxdy 中。我们要做的最后一件事是简单地更新绘制图像的轴的Position 字段。

现在,向您展示一些结果:

  1. size_factor 等于 1.0:
  2. size_factor 等于 1.4:
  3. size_factor 等于 0.55:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2013-06-11
    • 2011-09-03
    • 2013-11-27
    • 2018-08-03
    • 2021-02-10
    • 1970-01-01
    相关资源
    最近更新 更多