【问题标题】:Matlab axis error using imagesc使用imagesc的Matlab轴错误
【发布时间】:2015-06-05 12:55:35
【问题描述】:

我在 matlab 3014b 中使用imagesc 和一个常见的colorbar 绘制多个热图。这是我的代码:

a(1)= subplot('Position',[0.1, 0.65, 0.3, 0.3]);
data1 = rand(5);
imagesc(data1)
ax = gca; 
ax.XTick = [1 2 3 4 5 6];
ax.XTickLabel = {'0','0.1', '0.2', '0.3','0.4','0.5'};
ax.YTick = [1 2 3 4 5 6];
ax.YTickLabel = {'1','10', '100', '1000', '10000', '100000'};

a(2)= subplot('Position',[0.45, 0.65, 0.3, 0.3]);
data2 = rand(5);
imagesc(data2)
ax = gca; 
ax.XTick = [1 2 3 4 5 6];
ax.XTickLabel = {'0','0.1', '0.2', '0.3','0.4','0.5'};
ax.YTick = [1 2 3 4 5 6];
ax.YTickLabel = {'1','10', '100', '1000', '10000', '100000'};

h=colorbar;
set(h, 'Position', [.8 .135 .0581 .8150])
for i=1:2
      pos=get(a(i), 'Position');
      set(a(i), 'Position', [pos(1) pos(2)]);
end

但我收到以下错误:

Error using matlab.graphics.axis.Axes/set
While setting the 'Position' property of Axes:
Value must be a 4 element vector

不确定如何解决这个问题?谢谢!

【问题讨论】:

    标签: matlab


    【解决方案1】:

    错误就在这里:

    for i=1:2
          pos=get(a(i), 'Position');
          set(a(i), 'Position', [pos(1) pos(2)]); %// <--- here
    end
    

    您截断最后两个元素是否有特殊原因? Position 应该是一个 4 元素向量,其中前两个元素定义容器左下角到坐标区左下角的距离,第三个和第四个元素是坐标区的宽度和高度窗内。如果您打算保持所有轴的宽度/高度相同,请执行以下操作:

    for i=1:2
          pos=get(a(i), 'Position');
          set(a(i), 'Position', [pos(1) pos(2) 1 1]); %// Change
    end
    

    查看有关轴属性的 MathWorks 文档...特别是 Position 此处:http://www.mathworks.com/help/matlab/ref/axes-properties.html#zmw57dd0e52524

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多