Need data points in 'plot' filled with the same color as the line.

23 views (last 30 days)
I need the data points in a plot to be the same color as the line.
I tried specifying the line and data points as follows: "o-", "MarkerFaceColor","auto".
The result is a line with unfilled data points. The data points have the outline that is the same color as the line but the data points are not filled.
What am I missing?

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 9 Oct 2023
From the documentation of plot, sub-section MarkerFaceColor - "The "auto" option uses the same color as the Color property of the parent axes."
The color of the parent axes is white, see below -
x = 0:0.1:10;
y = sin(x);
figure
plot(x,y,'o-', 'MarkerFaceColor', 'auto')
get(gca,'Color')
ans = 1×3
1 1 1
Solution, specify the color -
col = [0 0 1];
figure
plot(x,y,'o-', 'Color', col, 'MarkerFaceColor', col)
or plot the curve first and then set the MarkerFaceColor to be the same as the Line Color via the handle -
figure
h=plot(x,y,'o-');
h.MarkerFaceColor = h.Color;
  3 Comments
Dyuman Joshi
Dyuman Joshi on 10 Oct 2023
You are welcome!
If my answer solved your problem, please consider accepting the answer.
Vitek Stepien
Vitek Stepien on 10 Sep 2024
I have a follow up question, is there an easy way of doing that if the variable I'm plotting is a 2D matrix, so it plots N different curves with the default colors? I would really hate to have to cycle through all of them in a for loop, manually defining each MarkerFaceColor to match the line color.
Example:
m = magic(4);
figure, tiledlayout(2,1), nexttile
plot(magic(4),'Marker','square','MarkerFaceColor','auto'), title 'This is simple but not what I need'
cmap = colororder();
nexttile, hold on
for N = 1:length(m)
plot(m(:,N),'Marker','square','MarkerFaceColor', cmap(N,:))
end
title 'This is what I want to see'

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties 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!