如何在MATLAB图形中显示x轴和y轴?

时间:2022-02-19 10:54:30

I am drawing a graph using the plot() function, but by default it doesn't show the axes.

我正在使用plot()函数绘制图形,但默认情况下它不显示坐标轴。

How do we enable showing the axes at x=0 and y=0 on the graph?

我们如何在图上显示x=0和y=0处的坐标轴?

Actually my graph is something like:如何在MATLAB图形中显示x轴和y轴?

实际上我的图是这样的:

And I want a horizontal line corresponding to y=0. How do I get that?

我想要一条与y=0对应的水平线。我怎么知道的?

9 个解决方案

#1


9  

By default, plot does show axes, unless you've modified some settings. Try the following

默认情况下,plot会显示坐标轴,除非您修改了一些设置。试试以下

hold on; % make sure no new plot window is created on every plot command
axes(); % produce plot window with axes
plot(% whatever your plot command is);
plot([0 10], [0 0], 'k-'); % plot the horizontal line

#2


5  

The poor man's solution is to simply graph the lines x=0 and y=0. You can adjust the thickness and color of the lines to differentiate them from the graph.

可怜的人的解决方案是简单地画出x=0和y=0的直线。您可以调整线条的厚度和颜色,以区别于图表。

#3


4  

If you want the axes to appear more like a crosshair, instead of along the edges, try axescenter from the Matlab FEX.

如果你想让坐标轴看起来更像一个十字线,而不是沿着边缘,试试Matlab FEX中的axescenter。

EDIT: just noticed this is already pointed out in the link above by Jitse Nielsen.

编辑:刚刚注意到这已经在上面的链接由Jitse Nielsen指出。

#4


3  

Maybe grid on will suffice.

也许grid on就足够了。

#5


3  

This should work in Matlab:

这在Matlab中应该是可行的:

set(gca, 'XAxisLocation', 'origin')

Options are: bottom, top, origin.

选项有:底部,顶部,原点。

For Y.axis:

Y.axis:

YAxisLocation; left, right, origin

#6


2  

@Martijn your order of function calls is slightly off. Try this instead:

@Martijn你的函数调用顺序稍微偏离了一点。

x=-3:0.1:3;
y = x.^3;
plot(x,y), hold on
plot([-3 3], [0 0], 'k:')
hold off

#7


2  

I know this is coming a bit late, but a colleague of mine figured something out:

我知道这来得有点晚,但我的一个同事想出了一个办法:

figure, plot ((1:10),cos(rand(1,10))-0.75,'*-')
hold on
plot ((1:10),zeros(1,10),'k+-')
text([1:10]-0.09,ones(1,10).*-0.015,[{'0' '1'  '2' '3' '4' '5' '6' '7' '8' '9'}])
set(gca,'XTick',[], 'XColor',[1 1 1])
box off

#8


0  

Inspired by @Luisa's answer, I made a function, axes0

受到@Luisa的回答的启发,我制作了一个函数axes0

x = linspace(-2,2,101);
plot(x,2*x.^3-3*x+1);
axes0

如何在MATLAB图形中显示x轴和y轴?

You can follow the link above to download the function and get more details on usage

您可以按照上面的链接下载这个功能并获得更多的使用细节

#9


-1  

Easiest solution:

最简单的解决方案:

plot([0,0],[0.0], xData, yData);

情节([0,0],[0.0],xData,yData);

This creates an invisible line between the points [0,0] to [0,0] and since Matlab wants to include these points it will shows the axis.

这在[0,0]到[0,0]的点之间创建了一条不可见的线,由于Matlab希望包含这些点,它将显示轴。

#1


9  

By default, plot does show axes, unless you've modified some settings. Try the following

默认情况下,plot会显示坐标轴,除非您修改了一些设置。试试以下

hold on; % make sure no new plot window is created on every plot command
axes(); % produce plot window with axes
plot(% whatever your plot command is);
plot([0 10], [0 0], 'k-'); % plot the horizontal line

#2


5  

The poor man's solution is to simply graph the lines x=0 and y=0. You can adjust the thickness and color of the lines to differentiate them from the graph.

可怜的人的解决方案是简单地画出x=0和y=0的直线。您可以调整线条的厚度和颜色,以区别于图表。

#3


4  

If you want the axes to appear more like a crosshair, instead of along the edges, try axescenter from the Matlab FEX.

如果你想让坐标轴看起来更像一个十字线,而不是沿着边缘,试试Matlab FEX中的axescenter。

EDIT: just noticed this is already pointed out in the link above by Jitse Nielsen.

编辑:刚刚注意到这已经在上面的链接由Jitse Nielsen指出。

#4


3  

Maybe grid on will suffice.

也许grid on就足够了。

#5


3  

This should work in Matlab:

这在Matlab中应该是可行的:

set(gca, 'XAxisLocation', 'origin')

Options are: bottom, top, origin.

选项有:底部,顶部,原点。

For Y.axis:

Y.axis:

YAxisLocation; left, right, origin

#6


2  

@Martijn your order of function calls is slightly off. Try this instead:

@Martijn你的函数调用顺序稍微偏离了一点。

x=-3:0.1:3;
y = x.^3;
plot(x,y), hold on
plot([-3 3], [0 0], 'k:')
hold off

#7


2  

I know this is coming a bit late, but a colleague of mine figured something out:

我知道这来得有点晚,但我的一个同事想出了一个办法:

figure, plot ((1:10),cos(rand(1,10))-0.75,'*-')
hold on
plot ((1:10),zeros(1,10),'k+-')
text([1:10]-0.09,ones(1,10).*-0.015,[{'0' '1'  '2' '3' '4' '5' '6' '7' '8' '9'}])
set(gca,'XTick',[], 'XColor',[1 1 1])
box off

#8


0  

Inspired by @Luisa's answer, I made a function, axes0

受到@Luisa的回答的启发,我制作了一个函数axes0

x = linspace(-2,2,101);
plot(x,2*x.^3-3*x+1);
axes0

如何在MATLAB图形中显示x轴和y轴?

You can follow the link above to download the function and get more details on usage

您可以按照上面的链接下载这个功能并获得更多的使用细节

#9


-1  

Easiest solution:

最简单的解决方案:

plot([0,0],[0.0], xData, yData);

情节([0,0],[0.0],xData,yData);

This creates an invisible line between the points [0,0] to [0,0] and since Matlab wants to include these points it will shows the axis.

这在[0,0]到[0,0]的点之间创建了一条不可见的线,由于Matlab希望包含这些点,它将显示轴。