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

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

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

I wanted to use the default colors so your second solution worked perfectly.
Thank you!
You are welcome!
If my answer solved your problem, please consider accepting the answer.
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

Community Treasure Hunt

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

Start Hunting!