【问题标题】:How to specify the axis size when plotting figures in Matlab?在 Matlab 中绘制图形时如何指定轴的大小?
【发布时间】:2013-09-30 22:32:56
【问题描述】:

假设 我在 MATLAB 中有 2 个图形,它们都绘制了大小为 (512x512) 的数据,但是一个图形正在由一个设置轴参数的外部程序绘制。另一个是由我绘制的(使用 imagesc)。 目前的数字,或者更确切地说,轴的大小不同,我的问题是,如何使它们相等?。 我的问题的原因是,我想将它们导出为 pdf 格式以包含在乳胶文档中,并且我希望它们具有相同的大小而无需进一步处理。

提前致谢,N

编辑:图片链接

figure 1: (big)

link to smaller figure (i.e. the one whose properties I would like to copy and apply to figure 1)

【问题讨论】:

  • 也许您可以链接到这两个图的示例?
  • 即使外部程序为一个图形定义了轴的初始大小,之后您应该能够使两个轴均匀化。只需保存手柄并将尺寸属性设置为相同?

标签: matlab axis figures


【解决方案1】:

为此使用linkaxes():

% Load some data included with MATLAB
load clown

% Plot a histogram in the first subplot
figure
ax(1) = subplot(211);
hist(X(:),100)

% Create second subplot
ax(2) = subplot(212);

现在链接两个子图的轴:

linkaxes(ax)

通过绘制第二个子图,第一个子图会适应

imagesc(X)

首先,您有以下内容:

然后:

仅将示例扩展到图像:

load clown
figure
imagesc(X)
h(1) = gca;

I = imread('eight.tif');
figure
imagesc(I)
h(2) = gca;

注意以第一个手柄的配置为准:

linkaxes(h)

【讨论】:

  • +1 非常好的 fcn,不知道!这是新功能吗?
  • @LuciusDomitiusAhenobarbus 这个函数已经存在了一段时间,从 R14 开始,mathworks.co.uk/help/releases/R14/techdoc/ref/linkaxes.html
  • -> MathWorks 产品的许可用户可以访问存档文档。感谢您提供信息,但我通常没有使用 mathworks 帐户登录 :)
  • 不幸的是(可能是由于我的经验不足),我无法根据您的建议调整我的问题。我有 2 个单独的数字,因为我想单独导出它们,所以使用“子图”并不适合。然而,函数 linkaxes 看起来确实很有希望。根据我在下面发布的内容,也许可以使用链接轴找到更优雅的解决方案?
  • @neil 您需要将轴的句柄存储在某处(类似于我对子图所做的),然后在这些句柄上调用链接轴。
【解决方案2】:

1.获取图形和轴的句柄,如下所示:

 %perhaps the easiest way, if you have just this one figure:
 myFigHandle=gcf;
 myAxHandle=gca;
 %if not possible, you have to search for the handles:
 myFigHandle=findobj('PropertyName',PropertyValue,...)
 %you have to know some property to identify it of course...
 %same for the axes!

2.设置属性,如下:

%set units to pixels (or whatever you prefer to make it easier to compare to the other plot)
set(myFigHandle, 'Units','pixels')
set(myAxHandle, 'Units','pixels')
%set the size:
set(myFigHandle,'Position',[x_0 y_0 width height]) %coordinates on screen!
%set the size of the axes:
set(myAxHandle,'Position',[x_0 y_0 width height])  %coordinates within the figure!

【讨论】:

    【解决方案3】:

    好的,根据@Lucius Domitius Ahenoba 的回答,这是我想出的:

    hgload('fig1.fig'); % figure whose axis properties I would like to copy
    hgload('fig2.fig');
    figHandles = get(0,'Children');
    figHandles = sort(figHandles,1); 
    ax(1) = findobj(figHandles(1),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
    ax(2) = findobj(figHandles(2),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
    
    screen_pos1 = get(figHandles(1),'Position');
    axis_pos1 = get(ax(1),'Position');
    
    set(figHandles(2),'Position',screen_pos1);
    set(ax(2),'Position',axis_pos1);
    

    这是“之前”的结果:

    这是“之后”的结果:

    几乎是正确的,只是纵横比仍然关闭。有人知道如何平衡与轴相关的所有内容吗? (我意识到我不应该在发布答案时提出问题,但是将以上内容添加为评论被证明有点笨拙!)

    【讨论】:

    • 您是否检查过两者的单位是否相同?然后你应该调试它,检查手柄是正确的
    • @LuciusDomitiusAhenobarbus 由于我的代码修改了图 2(在右上方),我假设句柄是正确的(?)。两个轴的单位也相同。
    猜你喜欢
    • 2014-08-22
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2019-12-10
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多