Hi everyone,
I have been searching for an easy way to draw the x and y axis on a plot but cannot seem to do it. For example, if I have a plot that has range [-2 10] for x and [-200 400] for y, I would like to be able to draw a line along x = 0 and y = 0. I have looked at certain functions such as axescenter but this draws the axes in the exact center so it does not work for me. The style is great but I can't get it along the axes. Anyone have any ideas?

 採用された回答

Walter Roberson
Walter Roberson 2012 年 6 月 12 日

6 投票

xL = xlim;
yL = ylim;
line([0 0], yL); %x-axis
line(xL, [0 0]); %y-axis

10 件のコメント

David Polcari
David Polcari 2012 年 6 月 12 日
That works great! Any chance I could get the line color to be black? And what if I want to make the line bolder?
Kevin Holst
Kevin Holst 2012 年 6 月 12 日
see the documentation under "line"
"Examples
This example uses the line function to add a shadow to plotted data. First, plot some data and save the line's handle:
t = 0:pi/20:2*pi;
hline1 = plot(t,sin(t),'k');
Next, add a shadow by offsetting the x-coordinates. Make the shadow line light gray and wider than the default LineWidth:
hline2 = line(t+.06,sin(t),'LineWidth',4,'Color',[.8 .8 .8]);"
Sean de Wolski
Sean de Wolski 2012 年 6 月 12 日
xL = xlim;
yL = ylim;
line(xL, [0 0],'color','k','linewidth',10) %x-axis
line([0 0], yL,'color','k','linewidth',10) %y-axis
And for more info
>>doc line
David Polcari
David Polcari 2012 年 6 月 12 日
Great! Thanks a lot for the help guys!
AnnArborObserver
AnnArborObserver 2016 年 9 月 1 日
I believe the comments about %x-axis and %y-axis are reversed and should be:
xL = xlim;
yL = ylim;
line([0 0], yL); %y-axis
line(xL, [0 0]); %x-axis
surya chandra gulipalli
surya chandra gulipalli 2019 年 5 月 12 日
great. Thank you
Emon Baroi
Emon Baroi 2019 年 9 月 26 日
thank you very much ...it can help me a lot
Cody Roman
Cody Roman 2021 年 2 月 18 日
Very helpful
Zhao Lu
Zhao Lu 2022 年 8 月 10 日
Thanks so much
Sidharth A Narayanan
Sidharth A Narayanan 2023 年 3 月 22 日
I think you interchanged the x-axis and y-axis ...

サインインしてコメントする。

その他の回答 (3 件)

Brian Russell
Brian Russell 2021 年 4 月 30 日

25 投票

Why does everyone give such complicated answers to this question? The answer is xline(0) and yline(0).

4 件のコメント

Elie Marouani
Elie Marouani 2021 年 5 月 18 日
編集済み: Elie Marouani 2021 年 5 月 18 日
This is exactly what i needed here, thank you
Sajib Biswas Shuvo
Sajib Biswas Shuvo 2021 年 6 月 21 日
This answer should be at the top. Thanks btw.
Brian Russell
Brian Russell 2021 年 6 月 21 日
Thanks, Sajib, but I don't know how to move it to the top. I think an administrator has to do that. I think that in an earlier version of MATLAB, the answer at the top was the correct one, but the answer I gave must be a new feature.
Alex Henderson
Alex Henderson 2023 年 2 月 23 日
"Why does everyone give such complicated answers to this question?"
Probably because these functions only appeared in R2018b. Still, useful to know they're there now.

サインインしてコメントする。

Steven Lord
Steven Lord 2016 年 9 月 1 日

5 投票

As of release R2015b you can set the axes XAxisLocation and YAxisLocation properties to 'origin' to get the axes lines to cross at the origin.

1 件のコメント

Ben Ralph
Ben Ralph 2019 年 6 月 23 日
Dead link now I'm afraid.
Now here.

サインインしてコメントする。

Kajeen Hassan
Kajeen Hassan 2020 年 1 月 28 日

0 投票

A parametric equation to plot the function x= 1.5 sin(5t), y= 1.5 cos(3t) , plot the function for 0<=t<=2* pi. Format the plot such that the both axes will range from -2 to 2.

カテゴリ

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by