How to generate barplot like this on a given data?
古いコメントを表示

This image is taken from the matlab examples. In the example bar plot is used but bar plot doesn't seem to have any [name, value] pair to represent the plot like this.
2 件のコメント
KALYAN ACHARJYA
2020 年 1 月 13 日
Sharan Magavi comment moved here-
Hello,
thank you for your response.
actually this is NOT a stacked plot.
The example i've taken is for sequence data and combining data into mini-batch sizes.
for example - if I have about 20 sequences of data and splitting it into batches of 4 then I want the plot to represent the bar plot of the sequences but simultenously want to highlight that 4 sequences make a single batch. The padding value is dependant on the sequence length in each batch.
say I have sequences of varying length - I sort it first and then when I make the mini- batches the padding occurs based on the max sequence length in every batch.
additionally, If I split the given sequences into a scalar value then the intermidiate red line( --) should be at the scalar value for each sequence of each batch.
hope this is not confusing.. for more information please check out LSTM sequence padding and truncation.
Chih
2021 年 12 月 10 日
The question is "HOW to create such plot"...
回答 (1 件)
Mathieu NOE
2021 年 12 月 13 日
hello
example of horizontal bar plot
clc
clearvars
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);
% 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.15;
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,'VerticalAlignment','middle');
xlabel('CO² concentration','FontSize',15,'HorizontalAlignment','left')
title('CO² concentration vs. GDP range','FontSize',15)
hcb=colorbar('hor');
hcb.Title.String = "GDP range";
hcb.Title.HorizontalAlignment = "right";
hcb.Title.VerticalAlignment = "cap";
hcb.Title.FontSize = 15;
2 件のコメント
Chih
2021 年 12 月 13 日
Thank you very much, Mathieu.
Mathieu NOE
2021 年 12 月 13 日
my pleasure !
カテゴリ
ヘルプ センター および File Exchange で Color and Styling についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!