在 MATLAB 中将 y 轴倒置 [英] Turning y axis upside down in MATLAB

查看:73
本文介绍了在 MATLAB 中将 y 轴倒置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 matlab 图中把 y 轴倒过来,这样 y 轴的正方向,而不是向上,指向下?

Is there a way to turn the y axis upside down in matlab plots, so that the positive direction of the y axis, instead of up, points down ?

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

(I beg of you; please do not say, print it out, then turn the paper around ;-)

推荐答案

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

The 'YDir' axes property can be either 'normal' or 'reverse'. By default it is 'normal' for most plots, but some plots will automatically change it to 'reverse', such as the image or imagesc functions.

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

You can set the y-axis direction of an axes with either the set function or dot indexing (in newer MATLAB versions):

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

我对其他一些回答感到困惑,他们说 'YDir' 属性不知何故消失或出现错误.我在 2013、2014 或 2016 年的 MATLAB 版本中没有看到任何此类行为.我只遇到了两个潜在的陷阱:

I'm baffled by some of the other answers saying that the 'YDir' property has somehow disappeared or is giving an error. I haven't seen any such behavior in versions of MATLAB from 2013, 2014, or 2016. There are only two potential pitfalls I came across:

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

  • The property can't be set with a cell array, only a character string:

>> 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' 属性,不管它的当前状态是什么,你可以这样做:

    Finally, if you want some code that will toggle the 'YDir' property no matter what its current state is, you can do this:

    set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
    % or...
    h = gca;
    h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));
    

    这篇关于在 MATLAB 中将 y 轴倒置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆