PLOTTING BAR GRAPHS with different colors

I have data stored in an array A from 1 to 400: A[1] =0, A[2]= 66.56, A[3]= 64.8, A[4]=56.8.....A[10] = 54.3, A[11]=73.3 ......... A[400] = 76.5
I wish to plot a bar graph in such a manner A[1] to A[10] in red color and A[11] to A[400] in blue color in a same graph. Can any one help me in this?

 採用された回答

Star Strider
Star Strider 2021 年 11 月 22 日

0 投票

Try this —
A = rand(1,400);
x = 1:400;
figure
hb = bar(x, A);
hb.FaceColor = 'flat';
hb.CData(1:10,:) = [ones(10,1) zeros(10,2)];
xlim([0 50]) % Zoom To See Detail (Delete Later)
Experiment to get different results.
,

3 件のコメント

Nitin Arora
Nitin Arora 2021 年 11 月 22 日
@Star Strider: what is graph is required in this format? In this like on x-axis the values and on y-axis percentage of samples.
Star Strider
Star Strider 2021 年 11 月 22 日
The legend was not part of the original request.
The only way to do that is to use the hold function and plot two separate bar objects. (I did some deep property spelunking and could not devise away to make the legend request compatible with my original code.)
A = rand(1,400);
x = 1:400;
figure
hold on
hb(1) = bar(x(1:10), A(1:10), 'r');
hb(2) = bar(x(11:end), A(11:end), 'b');
hold off
legend('Red Bars', 'Blue Bars', 'Location','bestoutside');
xlim([0 50]) % Zoom To See Detail (Delete Later)
.
dpb
dpb 2021 年 11 月 22 日
<Answers 1592474-plotting-bar-graphs-with-different-color> amended to add the legends to the previous/alternate solution.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 11 月 22 日

0 投票

N = 10;
bar(x(1:N), y(1:N), 'r');
hold on
bar(x(N+1:end), y(N+1:end), 'b');
hold off

カテゴリ

ヘルプ センター および File ExchangeDiscrete Data Plots についてさらに検索

製品

質問済み:

2021 年 11 月 22 日

コメント済み:

dpb
2021 年 11 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by