Can I put markers on only some of the points in my plot?

511 views (last 30 days)
I have a plot that has a lot of points. Putting markers on all the points makes the plot too cluttered; instead of a line made of markers I just get a thick line. How can I plot only every other marker, or every third marker, or something like that?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Jan 2016
There is no command to plot only every other marker, or every third marker, or every nth marker. However, you can accomplish this by using the following command, assuming you have your X-data stored in XVec and your Y-data stored in YVec:
plot(XVec(1:2:end),YVec(1:2:end), '+')
This command will plot every other point in your data using the + marker. Change the 2 in the statement to a 3 to plot every third point:
plot(XVec(1:3:end),YVec(1:3:end), '+')
or to a 4 to plot every fourth, etc:
plot(XVec(1:4:end),YVec(1:4:end), '+')
To plot a line with every fourth marker present, you can use the following code:
plot(XVec,YVec)
hold on
plot(XVec(1:4:end),YVec(1:4:end), '+')
hold off
Note that since the solid line and markers are different plots, they will be displayed as different items in the legend. A decision can be made to display only one of these items. See the linked solution for a description of how this can be achieved.
In addition, the request for a built-in MATLAB command to do this automatically has been forwarded to our development staff for consideration for a future version of MATLAB.
  3 Comments
Ali
Ali on 29 Oct 2017
if true
--------------------------------------------------- code start
This is an example for your case
Input is "Input_Data", two dimension matrix
Marker_Counter=1;
figure6=figure;
Markers = {'+','o','*','x','v','d','^','s','>','<'};
for i=1:10:size(Input_Data,1)
TPR=Input_Data(i:i+9,7);
FPR=Input_Data(i:i+9,8);
plot(FPR,TPR,strcat('-',Markers{Marker_Counter}));
Marker_Counter=Marker_Counter+1;
hold on
end
plot([0.5 1],[0.5 1],'--');
legend('Minpts = 100','Minpts = 200','Minpts = 300','Minpts = 400','Minpts = 500','Minpts = 600','Minpts = 700','Minpts = 800','Minpts = 900','Minpts = 1000','','Location','SouthEast');
xlabel('FPR or (1-Specificity)','FontSize',12,'FontWeight','bold'); ylabel('TPR or Spensitivity)','FontSize',12,'FontWeight','bold');
title('ROC Space');
close(gcf);
-------------------------------------------- code end
end
--------------------------------------- picture link preview

Sign in to comment.

More Answers (4)

Nirajan R
Nirajan R on 24 Sep 2016
Edited: MathWorks Support Team on 30 Dec 2021
The feature has been added now. You can use MarkerIndices property to achieve this. More info here: https://www.mathworks.com/help/matlab/creating_plots/create-line-plot-with-markers.html
  3 Comments
Steven Lord
Steven Lord on 4 Jun 2019
When you call plot, omitting part of the data can change the shape of the line that is drawn so to control which markers are plotted you need to use MarkerIndices.
When you call scatter, omitting part of the data does not impact the rest of the points. Therefore you can simply call scatter on only part of your data. If you switch between the two figures created by the example below, the only differences will be the titles and the five points which were omitted in the second figure.
x = rand(1, 25);
y = rand(1, 25);
figure
scatter(x, y)
axis([0 1 0 1])
title('All data')
figure
scatter(x(1:20), y(1:20))
axis([0 1 0 1])
title('Only first 20 points')

Sign in to comment.


Mohammad Sobhani
Mohammad Sobhani on 19 Nov 2018
You can use ''MarkerIndices" in plot options. You can set any matrix for indices that you want their markers will be visible.
plot(x, y, 'bs-', 'MarkerIndices', 1:10:length(x))
  2 Comments
Mohammad Sobhani
Mohammad Sobhani on 4 Jun 2019
It seems that scatter function doesn't have this feature. But you can use plot function instead of scatter function. For example:
scatter(x, y, 's')
plot(x, y, 's', 'MarkerIndices', 1:10:length(x))

Sign in to comment.


Jacob Jonsson
Jacob Jonsson on 22 May 2018
I wrote a function available at: https://www.mathworks.com/matlabcentral/fileexchange/37165-plotsparsemarkers Should work with MATLAB versions that have 'MarkerIndices' as well as the older ones.

Dobs
Dobs on 22 Nov 2021
For some reason, when I use MarkerIndices in my line plot, the actual line disappears and only the markers are left. Is there a way to add the markers while keeping the line?

Community Treasure Hunt

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

Start Hunting!