3,189 views (last 30 days)
Show older comments
benjamin ma on 27 Feb 2014
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot
Edited: MathWorks Support Team on 4 Mar 2024
Accepted Answer: Mischa Kim
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?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Mischa Kim on 1 Mar 2024
Edited: MathWorks Support Team on 4 Mar 2024
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-darray forthe '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":
https://www.mathworks.com/help/matlab/ref/plot.html#namevaluepairs
6 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
benjamin ma on 27 Feb 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_198934
Thank you. it works now. but one question, can i use "set" function to mark a plot also?
Matt Kindig on 27 Feb 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_198946
Open in MATLAB Online
Sure. First you will need to define the plot object, however. Something like this should work:
h = plot(NaN, NaN, 'r*'); %define the graphics object, but don't render anything.
set(h,'Xdata', x(10), 'YData', y(10)); %mark 10th point
pause(5); %after a few seconds...
set(h, 'XData', x(20), 'YData', y(20)); %mark 20th point instead.
Vijay shankar Sridharan on 4 Jan 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_1243048
Hi Mischa, I get error when I try to plot points with decimal points.
Walter Roberson on 4 Jan 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_1244483
Open in MATLAB Online
Could you expand on what you mean about plotting points with decimal points? For example are you trying to do
set(h, 'XData', x(20.3), 'YData', y(20.3)); %mark 20.3th point instead.
Ihaveaquest on 22 Aug 2022
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_2327000
Open in MATLAB Online
How would you set mltiple markers example shows marker at 10, what if i needed markers at 20 as well?
x = 0:0.1:pi;
y = sin(x);
p = plot(x,y,'o-','MarkerIndices',10)
Walter Roberson on 23 Aug 2022
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_2329130
Open in MATLAB Online
@Ihaveaquest
x = 0:0.1:pi;
y = sin(x);
p = plot(x,y,'o-','MarkerIndices', [10 20])
Sign in to comment.
More Answers (3)
Greg on 6 Dec 2017
Edited: Greg on 6 Dec 2017
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
MathWorks Support Team on 27 Nov 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_643142
Open in MATLAB Online
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)
Sign in to comment.
navi g on 9 Jan 2017
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Walter Roberson on 9 Jan 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/119402-how-do-i-add-a-marker-at-one-specific-point-on-a-plot#comment_418874
Open in MATLAB Online
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.
Sign in to comment.
Mourad on 1 Mar 2024
Comment afficher le noms de barre des nœuds sur mon dessin matlab?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and Annotation
Find more on Formatting and Annotation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office