How to set a marker at a desired location on a plot
12 次查看(过去 30 天)
显示 更早的评论
Hi,I'm trying to set a marker at integer values of x.I've used index method(x(3)) but couldn't do it.Because I used linspace function.How do I mark my graph when x is equal to an integer number?Thanks.
x=linspace(-4,4);
y=x.*exp(-x.^2);
plot(x,y)
end
0 个评论
采纳的回答
Star Strider
2016-12-25
The easiest way is to define a second vector with integer values, since by inspection only the endpoints of ‘x’ are integers:
x = linspace(-4,4);
xi = min(x) : max(x); % Integer ‘x’ Vector
y = x.*exp(-x.^2);
yi = xi.*exp(-xi.^2);
plot(x,y)
hold on
plot(xi, yi, 'rp', 'MarkerFaceColor','g')
hold off
grid
0 个评论
更多回答(2 个)
John BG
2016-12-25
use a vector index, let it be k, instead of the x reference of the plot.
1.
plot and add a marker
x=linspace(-4,4);
y=x.*exp(-x.^2);
plot(x,y)
hold on
p = plot(x(1),y(1),'o','MarkerFaceColor','red');
2.
now you can simply place the marker on one of the plotted points of y by varying k between 1 and numel(y), for instance:
k=20;
p.XData = x(k);
p.YData = y(k);
or
hold on;
p = plot(x(k),y(k),'o','MarkerFaceColor','red');
hold off;
the figure window also has a marker called Data Cursor.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help, please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
0 个评论
absomnia48
2016-12-25
2 个评论
Star Strider
2016-12-25
编辑:Star Strider
2016-12-26
You are almost there with the ‘%or this one’ version. You only need to add a function handle, here that would be ‘@(x)’:
fx = @(x) x.*exp(-x.^2);
Ymin = fx(fminbnd(fx, -4, 4))
Ymax = fx(fminbnd(@(x) -fx(x), -4, 4))
Ymin =
-428.8819e-003
Ymax =
428.8819e-003
Note that to get the maximum, take the minimum of the negative of the funciton.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!