Plotting a a constant line in a graph

67 views (last 30 days)
Quantopic
Quantopic on 29 Aug 2014
Answered: Adam on 29 Aug 2014
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.

Accepted Answer

Iain
Iain on 29 Aug 2014
plot(X,ones(size(X)) * k)

More Answers (1)

Adam
Adam on 29 Aug 2014
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).

Tags

Community Treasure Hunt

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

Start Hunting!