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 ?
有没有一种方法可以把y轴翻转到matlab中,这样y轴的正方向,而不是向上,向下?
(I beg of you; please do not say, print it out, then turn the paper around ;-)
(我请求你;请不要说,把它打印出来,然后把纸翻过来。
5 个解决方案
#1
53
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.
“YDir”轴属性可以是“正常”或“反向”。默认情况下,大多数情节都是“正常”的,但有些情节会自动将其改为“反向”,比如图像或图像功能。
You can set the y-axis direction of an axes with either the set
function or dot indexing (in newer MATLAB versions):
可以用set函数或点索引(在新的MATLAB版本中)设置坐标轴的y轴方向:
h = gca; % Handle to currently active axes
set(h, 'YDir', 'reverse');
% or...
h.YDir = 'reverse';
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:
我被一些其他的答案迷惑了,说“YDir”的属性不知怎么消失了,或者出现了错误。在2013年、2014年或2016年的MATLAB版本中,我从未见过这样的行为。我遇到的潜在陷阱只有两个:
-
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'.
although this works:
虽然这个工作原理:
set(gca, {'YDir'}, {'reverse'}); % Property name is also a cell array
-
The
gca
function can't be used interchangeably as a handle when performing dot indexing (which is why I first saved it to a variableh
in the above example):在执行点索引时,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'
Finally, if you want some code that will toggle the 'YDir'
property no matter what its current state is, you can do this:
最后,如果您需要一些代码来切换“YDir”属性,不管它当前的状态是什么,您可以这样做:
set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
% or...
h = gca;
h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));
#2
9
The command
命令
axis ij
轴ij
Will also reverse the Y-axis (negative above x-axis; positive below).
也会反转y轴(x轴上方为负);积极的下面)。
#3
6
The solutions on the top of the stack did not work for me,
堆栈顶部的解决方案对我不起作用,
-
imagesc(x,y,data) % results in a flipped plot, the y axis is upside down
imagesc(x,y,data) %结果在一个翻转的图中,y轴是颠倒的。
-
set(gca,'YDir','reverse'); % gives an error
集(gca、“YDir”“反向”);%给了一个错误
-
axis ij; % still gives the flipped plot
轴ij;%仍然给出翻转的图。
what did work was the following:
工作内容如下:
imagesc(x,y,data); axis xy; % results in the correct plot
The YDir
property has vanished in the matlab version (2013 and up) that I'm using.
YDir属性在我使用的matlab版本(2013和up)中消失了。
#4
2
To update this answer, since it is still a popular Google result: As of R2014a, the correct way to flip the Y axis is the following:
为了更新这个答案,因为它仍然是一个流行的谷歌结果:在R2014a中,翻转Y轴的正确方法如下:
>> axis ij
This change can be reversed through the following command
可以通过以下命令反转此更改。
>> axis ji
To flip the X or Z axes, do the following
要翻转X或Z轴,请执行以下操作。
set(gca,'XDir','reverse');
集(gca、“XDir”“反向”);
set(gca,'ZDir','reverse');
集(gca、“ZDir”“反向”);
Personally, I think it would have been easier to keep the YDir option, but what do I know.
就我个人而言,我认为保留YDir选项会更容易些,但我知道什么呢?
#5
0
As an alternative to YDir
(for some reason I cannot currently see) you can rotate the axes with view
. To turn the y-axis upside down, use
作为YDir的替代(出于某些原因,我目前无法看到),您可以使用视图旋转坐标轴。将y轴翻转过来,使用。
view(0,-90);
#1
53
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.
“YDir”轴属性可以是“正常”或“反向”。默认情况下,大多数情节都是“正常”的,但有些情节会自动将其改为“反向”,比如图像或图像功能。
You can set the y-axis direction of an axes with either the set
function or dot indexing (in newer MATLAB versions):
可以用set函数或点索引(在新的MATLAB版本中)设置坐标轴的y轴方向:
h = gca; % Handle to currently active axes
set(h, 'YDir', 'reverse');
% or...
h.YDir = 'reverse';
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:
我被一些其他的答案迷惑了,说“YDir”的属性不知怎么消失了,或者出现了错误。在2013年、2014年或2016年的MATLAB版本中,我从未见过这样的行为。我遇到的潜在陷阱只有两个:
-
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'.
although this works:
虽然这个工作原理:
set(gca, {'YDir'}, {'reverse'}); % Property name is also a cell array
-
The
gca
function can't be used interchangeably as a handle when performing dot indexing (which is why I first saved it to a variableh
in the above example):在执行点索引时,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'
Finally, if you want some code that will toggle the 'YDir'
property no matter what its current state is, you can do this:
最后,如果您需要一些代码来切换“YDir”属性,不管它当前的状态是什么,您可以这样做:
set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
% or...
h = gca;
h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));
#2
9
The command
命令
axis ij
轴ij
Will also reverse the Y-axis (negative above x-axis; positive below).
也会反转y轴(x轴上方为负);积极的下面)。
#3
6
The solutions on the top of the stack did not work for me,
堆栈顶部的解决方案对我不起作用,
-
imagesc(x,y,data) % results in a flipped plot, the y axis is upside down
imagesc(x,y,data) %结果在一个翻转的图中,y轴是颠倒的。
-
set(gca,'YDir','reverse'); % gives an error
集(gca、“YDir”“反向”);%给了一个错误
-
axis ij; % still gives the flipped plot
轴ij;%仍然给出翻转的图。
what did work was the following:
工作内容如下:
imagesc(x,y,data); axis xy; % results in the correct plot
The YDir
property has vanished in the matlab version (2013 and up) that I'm using.
YDir属性在我使用的matlab版本(2013和up)中消失了。
#4
2
To update this answer, since it is still a popular Google result: As of R2014a, the correct way to flip the Y axis is the following:
为了更新这个答案,因为它仍然是一个流行的谷歌结果:在R2014a中,翻转Y轴的正确方法如下:
>> axis ij
This change can be reversed through the following command
可以通过以下命令反转此更改。
>> axis ji
To flip the X or Z axes, do the following
要翻转X或Z轴,请执行以下操作。
set(gca,'XDir','reverse');
集(gca、“XDir”“反向”);
set(gca,'ZDir','reverse');
集(gca、“ZDir”“反向”);
Personally, I think it would have been easier to keep the YDir option, but what do I know.
就我个人而言,我认为保留YDir选项会更容易些,但我知道什么呢?
#5
0
As an alternative to YDir
(for some reason I cannot currently see) you can rotate the axes with view
. To turn the y-axis upside down, use
作为YDir的替代(出于某些原因,我目前无法看到),您可以使用视图旋转坐标轴。将y轴翻转过来,使用。
view(0,-90);