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

477 ビュー (過去 30 日間)
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?

採用された回答

MathWorks Support Team
MathWorks Support Team 2016 年 1 月 15 日
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 件のコメント
Ali
Ali 2017 年 10 月 29 日
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

サインインしてコメントする。

その他の回答 (4 件)

Nirajan R
Nirajan R 2016 年 9 月 24 日
編集済み: MathWorks Support Team 2021 年 12 月 30 日
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 件のコメント
Steven Lord
Steven Lord 2019 年 6 月 4 日
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')

サインインしてコメントする。


Mohammad Sobhani
Mohammad Sobhani 2018 年 11 月 19 日
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 件のコメント
Mohammad Sobhani
Mohammad Sobhani 2019 年 6 月 4 日
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))

サインインしてコメントする。


Jacob Jonsson
Jacob Jonsson 2018 年 5 月 22 日
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 2021 年 11 月 22 日
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!

Translated by