matlab - 多图 Matlab 包括图像
问题描述
我使用该tiledlayout(2,2)
命令在 Matlab 中创建绘图。前三个标题字段填充了正常的地块。第四个也是最后一个字段是带有图像的字段。我使用了命令IMG = 'mypic.tif'
和imshow(IMG)
。我想知道为什么我不能更改图像的大小。这不能用tiledlayout
命令吗?我已经查阅了关于tiledlayout
and的纪录片imshow()
,但没有发现任何对我有帮助的东西。
解决方案
确实似乎使用该功能是不可能的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
绘制图像的轴的字段。
现在,向您展示一些结果:
推荐阅读
- python - 为什么当我在 python 中按下按钮“运行”时它没有给出任何输出?
- unity3d - 自定义着色器不接收光线
- flutter - Flutter web TextField 不显示写在键盘上的文本
- python - 一种能够消除 N 个障碍物的星型算法
- pic - 无法在 MPLABX (PKOB) 上使用 Curiosity HPC,如何让它在当前 IDE 上工作?
- html - 小图标图像添加到 span 类里面
- 标签?
- html - Italic tag not being formatted in Github Pages
- python - 使用 Python 调用 JSON API
- firebase - 如何将 Google App Engine 与 Firebase 结合使用
- php - Symfony 翻译忽略参数中的花括号