在MATLAB中将y轴上下颠倒 [英] Turning y axis upside down in MATLAB
问题描述
是否有一种方法可以在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 ;-)
推荐答案
image
或
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.
您可以使用 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
的原因):
The gca
function can't be used interchangeably as a handle when performing dot indexing (which is why I first saved it to a variable h
in the above example):
>> 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屋!