필터 지우기
필터 지우기

How to choose color in bar graph

조회 수: 5 (최근 30일)
Sangesh Pv
Sangesh Pv 2023년 11월 10일
답변: Voss 2023년 11월 10일
I am trying to fix specific colors in my bar graph but i dont know how i can choose multiple colors , red, blue, because each time i run output it changes its color.
clear all
data = readmatrix("4guploadtim.csv")
data = 20×2
1.0000 8.8100 2.0000 2.7900 3.0000 31.2000 4.0000 16.9000 5.0000 29.1000 6.0000 28.8000 7.0000 34.0000 8.0000 46.0000 9.0000 36.8000 10.0000 21.9000
data2 = readmatrix("4guploadvodafone.csv")
data2 = 20×2
1.0000 12.1000 2.0000 8.0300 3.0000 66.4000 4.0000 70.4000 5.0000 69.5000 6.0000 30.5000 7.0000 29.6000 8.0000 49.3000 9.0000 31.4000 10.0000 40.1000
x = data(:,1);
y = data(:,2);
hold on ;
x2 = data2(:,1);
y2 =data2(:,2);
h = bar([x,x2],[y,y2]);
hold off ;
set(gca,"XGrid","on","YGrid","off")
legend (h,"Tim","Vodafone")
xlabel('Location');
ylabel('Upload');
title('Upload speed vodafone 4g upload bar plot (Tim&Vodafone)');
i want the colors to be locked and not change for each run.
  댓글 수: 1
Rik
Rik 2023년 11월 10일
What do you mean that the colors change? If you have a fresh axes, the colors should be the same.
Ohterwise, a bar chart follows the colororder. Did you check the documentation to see how you can set the colors?

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2023년 11월 10일
The colors appear to change on each run because you're plotting into the same axes (with hold on) each time.
It's the same as plotting several lines without specifying their colors (MATLAB picks the colors based on the axes' ColorOrder property):
hold on
plot(1:10)
plot(2:11)
plot(3:12)
Except in your case the data is always the same, so each newly plotted set of bars is in exactly the same place as the old one, so you only see the last one, which has some new colors.
One way to avoid this is to create a new figure in your code before plotting the bars, so that each run plots into a different figure (hold on and hold off are not necessary in this case):
figure();
x = data(:,1);
y = data(:,2);
% hold on ; % hold on and hold off have no effect in this case and can be removed
x2 = data2(:,1);
y2 =data2(:,2);
h = bar([x,x2],[y,y2]);
% hold off ;
set(gca,"XGrid","on","YGrid","off")
legend (h,"Tim","Vodafone")
xlabel('Location');
ylabel('Upload');
title('Upload speed vodafone 4g upload bar plot (Tim&Vodafone)');
If instead of a new figure for each run, you want to always plot into the same figure, then you can put your hold off before you call bar, so that bar replaces whatever was in the axes (hold on is not necessary in this case):
x = data(:,1);
y = data(:,2);
x2 = data2(:,1);
y2 =data2(:,2);
hold off % turn hold off so that the bars replace whatever was in the axes
h = bar([x,x2],[y,y2]); % create the bars
set(gca,"XGrid","on","YGrid","off")
legend (h,"Tim","Vodafone")
xlabel('Location');
ylabel('Upload');
title('Upload speed vodafone 4g upload bar plot (Tim&Vodafone)');

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by