Multiple Colors in Bar Graph
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
I want a bar graph that creates 10 different bars. I want every bar to be different color. How do I achieve this?
採用された回答
Mathieu NOE
2021 年 11 月 18 日
hello
see example below
clc
clear all
close all
data_co2 = [.142 .156 .191 .251 0.5 0.86 2.2 4 8.3];
data_gdp = rand(size(data_co2));
uniNames = {'eno','pck','zwf','foo','bar','jhy','vfd','vre','zqs'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% main code %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_min = min(data_gdp);
data_max = max(data_gdp);
map = colormap('jet');
[mmap,nmap] = size(map);
f = figure(1);
N = numel(data_co2);
for i=1:N
h = bar(N-i+1, data_co2(i));
if i == 1, hold on, end
% now define col value based on data value (min data value maps to colormap map index 1
% and max data value maps to colormap map last index);
ind = fix(1+(mmap-1)*(data_gdp(i)-data_min)/(data_max-data_min));
set(h, 'FaceColor', map(ind,:)) ;
% Display the values as labels at the tips of the bars.
xtips1 = h.XEndPoints;
ytips1 = h.YEndPoints + 0.25;
labels1 = string(h.YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center')
end
%
set(gca, 'XTickLabel', '')
xlabetxt = uniNames;
xpos = -max(xlim)/25;
t = text(1:N,repmat(xpos,N,1), xlabetxt,'Rotation',45,'FontSize',15,'HorizontalAlignment','center');
ylabel('CO² concentration','FontSize',12)
title('CO² concentration vs. GDP range','FontSize',15)
hcb=colorbar('ver');
hcb.Title.String = "GDP range";
hcb.Title.FontSize = 13;

6 件のコメント
Vartika Agarwal
2021 年 11 月 19 日
This code is not running properly
Mathieu NOE
2021 年 11 月 19 日
hello
what matlab release are you running ?
can you tell what is the error message that appears in your command window ?
Vartika Agarwal
2021 年 11 月 19 日
I am using Matlab 2018a
Error in bar (line 23)
h = bar(N-i+1, data_co2(i));
I want a bar graph that that creates 10 different bars. I want every bar to be different color.
y=[20 10 5 3 12 18 25 20 30 40];
bar(y)
Mathieu NOE
2021 年 11 月 19 日
ok
can you check first that you actually have bar installed (should be as it's a basic graphical function)
let's try something simpler

y=[20 10 5 3 12 18 25 20 30 40];
N = length(y);
map = colormap(jet(N));
figure(1);
hold on
for i=1:N
h = bar(i, y(i));
set(h, 'FaceColor', map(i,:)) ;
end
hold off
Vartika Agarwal
2021 年 11 月 19 日
Thanks Sir
It really works
Mathieu NOE
2021 年 11 月 19 日
My pleasure !
would you mind accepting my answer ?
tx
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Bar Plots についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
