如何在Matlab上一起旋转图像和轴?

时间:2016-10-07 17:58:16

标签: matlab rotation matlab-figure axes

垂直和/或水平翻转的代码1不会影响axes(); 代码2,其中建议的解决方案不会产生预期的输出

close all; clear all; clc;
x = [5 8];
y = [3 6];
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
C2 = C(:,end:-1:1,:);           %# horizontal flip
C3 = C(end:-1:1,:,:);           %# vertical flip
C4 = C(end:-1:1,end:-1:1,:);    %# horizontal+vertical flip

% https://stackoverflow.com/a/4010203/54964
subplot(2,2,1), imagesc(x,y,C)
subplot(2,2,2), imagesc(x,y,C2)
subplot(2,2,3), imagesc(x,y,C3)
subplot(2,2,4), imagesc(x,y,C4)

%% Rotations of axes() unsuccessfully 
% https://stackoverflow.com/a/15071734/54964
figure
subplot(2,2,1), imagesc(x,y,C)
x = linspace(1, size(C, 2), numel(x)); % reverse only x
set(gca, 'XTick', x, 'XTickLabel', x)
subplot(2,2,2), imagesc(x,y,C2)

x = linspace(1, size(C, 2), numel(x)); % reverse back x
set(gca, 'XTick', x, 'XTickLabel', x)  % reverse y
y = linspace(1, size(C, 1), numel(y));
set(gca, 'YTick', y, 'YTickLabel', flipud(y(:)))
subplot(2,2,3), imagesc(x,y,C3)

x = linspace(1, size(C, 2), numel(x)); % now both x,y reversed
set(gca, 'XTick', x, 'XTickLabel', x)
subplot(2,2,4), imagesc(x,y,C4)

图。 1输出轴保持不变但图像正确翻转, 图2移动xticks / yticks

时的尝试输出

enter image description here enter image description here

预期输出

  • 图1(左上角)所有轴都正确
  • 图2(右上)y轴正确但x轴为8至5
  • 图3(左下)y轴从6到3但x轴正确
  • 图4(右下)y轴正确但x轴为3至6

尝试2

代码

% 1 start of vector 2 end of vector 3 length of vector 
figure
subplot(2,2,1), imagesc(x,y,C)
x = linspace(size(C, 2), 1, numel(x)); % reverse only x
set(gca, 'XTick', x, 'XTickLabel', x)
subplot(2,2,2), imagesc(x,y,C2)

x = linspace(1, size(C, 2), numel(x)); % reverse back x
set(gca, 'XTick', x, 'XTickLabel', x)  
y = linspace(size(C, 1), 1, numel(y)); % reverse y
set(gca, 'YTick', y, 'YTickLabel', flipud(y(:)))
subplot(2,2,3), imagesc(x,y,C3)

x = linspace(size(C, 2), 1, numel(x)); % now both x,y reversed
set(gca, 'XTick', x, 'XTickLabel', x)
y = linspace(1, size(C, 1), numel(y)); % reverse y
set(gca, 'YTick', y, 'YTickLabel', flipud(y(:)))
subplot(2,2,4), imagesc(x,y,C4)

输出

Error using matlab.graphics.axis.Axes/set
While setting the 'XTick' property of 'Axes':
Value must be a vector of type single or double whose values increase

Error in test_imagesc_subplot_figure (line 26)
set(gca, 'XTick', x, 'XTickLabel', x)

Eskapp的提案

我做了以下但没有成功,但没有改变图2;第一行数字保持xaxis的递增顺序;我也试过而不是reverse - normal

figure
subplot(2,2,1), imagesc(x,y,C)
x = linspace(1, size(C, 2), numel(x)); % reverse only x
set(gca,'xdir','reverse')
subplot(2,2,2), imagesc(x,y,C2)
  • 图1和图2 axis的输出保持不变

    enter image description here

学习EBH' answer

使用set(gca,'XTick',x,'XTickLabel',x, 'YTick',y,'YTickLabel',fliplr(y))变量y=linspace(0,180,181); x=0:0.5:10

时,y轴标签中的输出

enter image description here

Matlab:2016a
操作系统:Debian 8.5 64位
硬件:华硕Zenbook UX303UA

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,那么此代码可以满足您的需求:

x = 5:8;
y = 3:6;
C = reshape(0:2:22,4,3).';
C2 = fliplr(C); % horizontal flip
C3 = flipud(C); % vertical flip
C4 = rot90(C,2); % horizontal+vertical flip

% the answer starts here:
subplot(2,2,1), imagesc(x,y,C)
set(gca,'XTick',x,'XTickLabel',x,...
     'YTick',y,'YTickLabel',y)
subplot(2,2,2), imagesc(x,y,C2)
set(gca,'XTick',x,'XTickLabel',fliplr(x),...
     'YTick',y,'YTickLabel',y)
subplot(2,2,3), imagesc(x,y,C3)
set(gca,'XTick',x,'XTickLabel',x,...
     'YTick',y,'YTickLabel',fliplr(y))
subplot(2,2,4), imagesc(x,y,C4)
set(gca,'XTick',x,'XTickLabel',fliplr(x),...
     'YTick',y,'YTickLabel',fliplr(y))

结果:

rot_axes

我从2元素向量中更改了您xy,但是如果符合以下条件,它也会有效:

x = [5 8];
y = [3 6];

... BTW

您可以写下:

而不是操纵C并创建C2 ... C4
subplot 221, imagesc(x,y,C)
subplot 222, imagesc(fliplr(x),y,C)
subplot 223, imagesc(x,fliplr(y),C)
subplot 224, imagesc(fliplr(x),fliplr(y),C)

并在每次调用subplot之后在轴上添加操作。

修改

使用矢量的大小和限制:

x = linspace(0,10,6);
y = linspace(0,180,19); % no need to plot each label
N = 3613;
C = diag(1:N)*ones(N)+rot90(diag(1:N)*ones(N)); % some arbitrary matrix 

上面所有其余代码保持不变,我得到以下结果:

rot_ax2