I have x rows and three columns: Region, CO2, and GDP. I want to make a bar plot like below, using CO2 as x axis and Region as y axis, meanwhile, using the value of GDP to fill each bar and also add a color bar. I tried different ways, but I could not figure it out. Anyone could help me? Many thanks~

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 1월 15일

0 개 추천

hello
see code example below
all the best
clc
clear all
close all
data = [.142 .156 .191 .251 0.5 0.86 2.2 4 8.3];
uniNames = {'eno','pck','zwf','foo','bar','jhy','vfd','vre','zqs'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% main code %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_min = min(data);
data_max = max(data);
map = colormap('jet');
[mmap,nmap] = size(map);
f = figure(1);
fig_pos = [681 105 683 874];
set(f,'Position',fig_pos);
N = numel(data);
for i=1:N
h = barh(N-i+1, data(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(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.YEndPoints + 0.1;
ytips1 = h.XEndPoints;
labels1 = string(h.YData);
text(xtips1,ytips1,labels1,'VerticalAlignment','middle')
end
%
set(gca, 'YTickLabel', '')
ylabetxt = uniNames;
xpos = -max(ylim)/12;
text(repmat(xpos,N,1),1:N, ylabetxt','Rotation',0,'FontSize',15);
xlabel('CO² concentration','FontSize',12)
hcb=colorbar('hor');
hcb.Title.String = "A colorbar title";
% hcb.Title.HorizontalAlignment = "center";
hcb.Title.FontSize = 15;
hcb.Title.Position = [210 -40 0];

댓글 수: 3

hum - improved version with Co² and GDP data , so that color is mapped to GDP and bar length is mapped with C0²
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);
fig_pos = [681 105 683 874];
set(f,'Position',fig_pos);
N = numel(data_co2);
for i=1:N
h = barh(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.YEndPoints + 0.1;
ytips1 = h.XEndPoints;
labels1 = string(h.YData);
text(xtips1,ytips1,labels1,'VerticalAlignment','middle')
end
%
set(gca, 'YTickLabel', '')
ylabetxt = uniNames;
xpos = -max(ylim)/12;
text(repmat(xpos,N,1),1:N, ylabetxt','Rotation',0,'FontSize',15);
xlabel('CO² concentration','FontSize',12)
hcb=colorbar('hor');
hcb.Title.String = "GDP range";
% hcb.Title.HorizontalAlignment = "center";
hcb.Title.FontSize = 15;
hcb.Title.Position = [210 -40 0];
H.Zheng
H.Zheng 2021년 1월 16일
Hi, Mathieu. It works well, thank you so much~
Mathieu NOE
Mathieu NOE 2021년 1월 18일
you're welcome

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

질문:

2021년 1월 15일

댓글:

2021년 1월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by