32

有没有办法在 matlab 图中将 y 轴倒置,以便 y 轴的正方向,而不是向上,指向下方?

(求求你了;请不要说,打印出来,然后把纸翻过来;-)

4

5 回答 5

54

'YDir'轴属性可以是'normal''reverse'。默认情况下,它'normal'适用于大多数绘图,但有些绘图会自动将其更改为'reverse',例如imageorimagesc函数。

set您可以使用函数或点索引(在较新的 MATLAB 版本中)设置轴的 y 轴方向:

h = gca;  % Handle to currently active axes
set(h, 'YDir', 'reverse');
% or...
h.YDir = 'reverse';

我对其他一些答案感到困惑,说该'YDir'属性以某种方式消失或出现错误。我在 2013、2014 或 2016 年的 MATLAB 版本中没有看到任何此类行为。我只遇到了两个潜在的陷阱:

  • 该属性不能使用元胞数组设置,只能使用字符串:

    >> set(gca, 'YDir', {'reverse'});
    Error using matlab.graphics.axis.Axes/set
    While setting property 'YDir' of class 'Axes':
    Invalid enum value. Use one of these values: 'normal' | 'reverse'.
    

    虽然这有效:

    set(gca, {'YDir'}, {'reverse'});  % Property name is also a cell array
    
  • 在执行点索引时,该gca函数不能作为句柄互换使用(这就是我h在上面的示例中首先将其保存到变量中的原因):

    >> gca.YDir
    Undefined variable "gca" or class "gca.YDir". 
    >> gca.YDir = 'reverse'  % Creates a variable that shadows the gca function
    gca = 
      struct with fields:
    
        YDir: 'reverse'
    

最后,如果你想要一些代码来切换'YDir'属性,不管它的当前状态是什么,你可以这样做:

set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
% or...
h = gca;
h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));
于 2009-11-19T03:05:32.900 回答
9

命令

axis ij

还将反转 Y 轴(x 轴上方为负;下方为正)。

于 2013-02-14T00:28:51.820 回答
6

堆栈顶部的解决方案对我不起作用,

  • imagesc(x,y,data) % results in a flipped plot, the y axis is upside down

  • set(gca,'YDir','reverse'); % gives an error

  • axis ij; % still gives the flipped plot

起作用的是:

imagesc(x,y,data); axis xy;  % results in the correct plot

YDir属性在我使用的 matlab 版本(2013 及更高版本)中消失了。

于 2015-05-11T08:45:20.723 回答
2

更新这个答案,因为它仍然是一个流行的谷歌结果:截至 R2014a,翻转 Y 轴的正确方法如下:

>> axis ij

可以通过以下命令反转此更改

>> axis ji

要翻转 X 或 Z 轴,请执行以下操作

set(gca,'XDir','reverse');

set(gca,'ZDir','reverse');

就个人而言,我认为保留 YDir 选项会更容易,但我知道什么。

于 2015-05-22T21:59:10.833 回答
0

YDir作为(由于某种原因我目前看不到)的替代方法,您可以使用view. 要将 y 轴倒置,请使用

view(0,-90);
于 2017-09-11T14:09:32.207 回答