Plotting a a constant line in a graph

66 次查看(过去 30 天)
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