Plotting a a constant line in a graph

조회 수: 66 (최근 30일)
Quantopic
Quantopic 2014년 8월 29일
답변: Adam 2014년 8월 29일
Hi all,
I need for plotting a constant line in a chart in which there is a time series X. So, given the time series X, texting in the command window the code:
plot(X)
I get the time series plot. How can I add to the graph a constant line k = 2? Thanks for help.

채택된 답변

Iain
Iain 2014년 8월 29일
plot(X,ones(size(X)) * k)

추가 답변 (1개)

Adam
Adam 2014년 8월 29일
plot( [X(1) X(end), [k k] )
would do the job visually if there isn't a reason you need to have a point for every value of your X time series.
Plotting e.g. 1000 points to define a horizontal line is unlikely to result in any kind of performance hit, but is a little un-necessary when 2 points would do just as well.
You can also use:
xlim = get( hAxes, 'Xlim' )
plot( xlim, [k k] )
if you simply want a horizontal line to span the full current x axis range irrespective of whether or not you have clipped the x limits to your X vector (though this is not advisable if you have zoomed in of course as then your horizontal line will be too short when you zoom out again).

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by