MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中将y轴上下颠倒 (https://www.labfans.com/bbs/showthread.php?t=22921)

poster 2019-12-10 16:49

在MATLAB中将y轴上下颠倒
 
有没有办法在Matlab图中将y轴上下颠倒,以使y轴的正方向而不是向上指向下方?

(求求你;请不要说,先将其打印出来,然后再将纸张翻过来;-)


回答:
[URL="https://www.mathworks.com/help/matlab/ref/axes-properties.html#property_XDir"]'YDir'轴属性[/URL]可以是'normal'或'reverse' 。默认情况下,对于大多数图而言,它是'normal'的,但是某些图会自动将其更改为'reverse' ,例如[URL="https://www.mathworks.com/help/matlab/ref/image.html"]image[/URL]或[URL="https://www.mathworks.com/help/matlab/ref/imagesc.html"]imagesc[/URL]函数。

您可以使用[URL="https://www.mathworks.com/help/matlab/ref/set.html"]set[/URL]功能或点索引(在较新的MATLAB版本中) [URL="https://www.mathworks.com/help/matlab/ref/set.html"]set[/URL]轴的y轴方向:

h = gca; % Handle to currently active axes set(h, 'YDir', 'reverse'); % or... h.YDir = 'reverse'; 我对其他一些答案感到困惑,这些答案说'YDir'属性以某种方式消失或出现错误。在2013年,2014年或2016年的MATLAB版本中,我还没有看到任何此类行为。我遇到了两个潜在的陷阱:
[LIST][*]该属性不能用单元格数组设置,只能用字符串设置:

>> 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[*]在执行点索引时, [URL="https://www.mathworks.com/help/matlab/ref/gca.html"]gca[/URL]函数不能作为句柄互换使用(这就是为什么我在上面的示例中首先将其保存到变量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'[/LIST]最后,如果您需要一些代码来切换'YDir'属性,无论其当前状态如何,都可以执行以下操作:

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

[url=https://stackoverflow.com/questions/1760614]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 10:40

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.