FaceColor changes automatically in a for loop

23 ビュー (過去 30 日間)
guohua
guohua 2016 年 8 月 25 日
回答済み: Walter Roberson 2016 年 8 月 25 日
I find that the facecolor changes automatically when I use plot in a for loop.
base_color = colormap(lines(10));
x = 1:100;
figure
for i = 1:10
y1 = i*ones(1,length(x));
y2 = i*ones(1,length(x))+0.5;
hold on
plot(x,y1,'LineWidth',1.5,'MarkerFaceColor',base_color(i,:));
plot(x,y2,'LineWidth',1.5,'MarkerFaceColor',base_color(i,:));
end
hold off
It should be the same color for each two lines, but it's not. How can I plot the lines using the designated colors in 'base_color'?
I find not only the 'plot' function, but other functions like 'histogram' will be also like this.
If anyone can help, many thanks!
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 8 月 25 日
Which MATLAB version are you using, and which operating system?

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

採用された回答

KSSV
KSSV 2016 年 8 月 25 日
plot(x,y1,'LineWidth',1.5,'MarkerFaceColor',base_color(i,:));
In the above line the color which you are giving MATLAB is not taking it. So MATLAB is giving color on its own. You have to mention the color you want. Eg:
plot(x,y1,'color','b','LineWidth',1.5); % plot is blue
plot(x,y1,'color',base_color(i,:),'LineWidth',1.5); % in your case
  1 件のコメント
guohua
guohua 2016 年 8 月 25 日
Yes, it works! I have made such a stupid mistake... Thank you very much!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 8 月 25 日
I was able to reproduce this on R2016a in OS-X.
You have a few bugs in your code:
  • You are not requesting that markers be drawn. Markers are not drawn unless you specify a marker style either implicitly with a linespec such as '*' or explicitly with 'MarkerStyle'
  • You are not specifying that the line color should be 'none': default with hold on in R2014b and later is to use the next color in sequence;
  • You are specifying marker face color, not a marker edge color; and
  • You are not specifying that the edge color should be 'none': default is the same as the line color; and
  • You are not specifying a marker that needs much filling. Marker edge color dominates for small markers.
Possibly what you are really wanting to do is specify the Color (line color) instead of the marker face color.

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by