How do I add a marker at one specific point on a plot?

3,617 次查看(过去 30 天)
I have the following plot:
I annotated a red dot at the point I would like to mark. How can I add a marker at this point?

采纳的回答

Mischa Kim
Mischa Kim 2024-3-1
编辑:MathWorks Support Team 2024-3-4
You can add a marker in the following ways:
  • Plot the point itself: 
    hold on % to plot on the current figure
    plot(x_pos,y_pos,'r*') % adds a red asterisk at the point (x_pos,y_pos)
  • Specify a value for the 'MarkerIndices' property in plot to plot a line with markers at specific data points. For example, if 'x' is your x-axis data, 'y' is your y-axis data, and you would like to create a marker at the 10th (x,y) point:
    p = plot(x,y,'o-','MarkerFaceColor','red','MarkerEdgeColor','red','MarkerIndices',10)
  • Specify a 1-d array for the 'MarkerIndices' property to add multiple markers to the plotted line. For example, create markers at the first five points:
    p = plot(x,y,'o-','MarkerIndices', [1 2 3 4 5])
    p = plot(x,y,'o-','MarkerIndices', 1:5)
See the description of the 'MarkerIndices' property under the "Name-Value Arguments" section of the documentation page for "plot": 
  8 个评论
Md Affan
Md Affan 2024-7-2
But how can I remove this from a legend? We're clearly making a new line (or dot, in this case), and we don't want to have this in our legend?
Florian
Florian 2024-11-14,10:30
legend(AutoUpdate="off") % is what you are searching
p = plot(x,y,'o-','MarkerIndices', [10 20])
with this code the marker is not in the legend.

请先登录,再进行评论。

更多回答(3 个)

Greg
Greg 2017-12-6
编辑:Greg 2017-12-6
With 7k views in a month, I'm surprised this hasn't been updated.
Starting in R2016b, there is a MarkerIndices property. Instead of the other answer's suggested:
plot(x,y);
hold on;
plot(x(10),y(10),'r*');
Now simply use:
h = plot(x,y,'MarkerIndices',10);
Move the marker(s) around at any time if you've stored the handle h:
h.MarkerIndices = 5:5:length(x);
  1 个评论
MathWorks Support Team
In addition, you can specify the ‘o-‘ line style, which creates a solid line and markers. You can also specify marker properties, such as the face color and edge color.
x = linspace(0,pi,30);
y = sin(x);
p = plot(x,y,'o-','MarkerFaceColor','red','MarkerEdgeColor','red','MarkerIndices',10)

请先登录,再进行评论。


navi g
navi g 2017-1-9
hello, is this marking in plot is possible without writing code, and putting mark in plot in figure editor,
and for sinosoidal curve i have only x data, i dont have y data, but i need to mark on curve, means that i will give x value, based on x value it should place on curve exactly on sinosoidal curve,
  1 个评论
Walter Roberson
Walter Roberson 2017-1-9
For R2014b or later (I would have to check about earlier; I see some references in 2012 time frame) you can use data brushing. Click on the paintbrush in the figure and then you can click on a point to mark it.
For marking a particular location given only the x, then
x_to_mark = SomeSpecificXValue;
all_lines = findobj(gca, 'type', 'line');
number_of_marks = 0;
where_to_mark = [];
for K = 1 : length(all_lines)
this_line = all_lines(K);
this_xdata = get(this_line, 'XData');
if x_to_mark < min(this_xdata) | x_to_mark > max(this_xdata)
continue; %the line does not span that x
end
x_diff = diff(this_xdata);
if isempty(x_diff)
fprintf('skipping line #%d that is single point\n', K);
elseif all(x_diff > 0) | all(x_diff < 0)
%it is monotonic, safe to do interpolation
this_ydata = get(this_line, 'YData');
y_to_mark = interp1(this_xdata, this_ydata, x_to_mark);
number_of_marks = number_of_marks + 1;
where_to_mark(number_of_marks,:) = [x_to_mark, y_to_mark];
else
fprintf('skipping line #%d with unsorted x data\n', K);
end
end
if number_of_marks == 0
fprintf('That x was not found on a line we could handle\n');
else
hold on
plot(where_to_mark(:,1), where_to_mark(:,2), 'r*');
end
This is probably a lot longer than you were expecting. You did not happen to provide information that we might potentially have used to make it shorter. For example if you know there is only exactly one line, and that the x were sorted when you plotted, and that the x value to mark is definitely in range, then the code could be made much shorter.

请先登录,再进行评论。


Mourad
Mourad 2024-3-1
Comment afficher le noms de barre des nœuds sur mon dessin matlab?

类别

Help CenterFile Exchange 中查找有关 Formatting and Annotation 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by