Plotting histogram with percentage

Hi all,
Recently I had a csv that I want to import by
T = readtable('1.csv');
and plot by histogram
Any chance to display this histogram with indivudual percentage over each bar?
Class; Value; Class start; Class end;
1;8410;-0.039916738868;-0.032423677233;
2;78271;-0.032423677233;-0.024930615599;
3;501926;-0.024930615599;-0.017437553965;
4;149453;-0.017437553965;-0.009944492330;
5;342924;-0.009944492330;-0.002451430696;
6;546900;-0.002451430696;0.005041630939;
7;537726;0.005041630939;0.012534692573;
8;527320;0.012534692573;0.020027754207;
9;478488;0.020027754207;0.027520815842;
10;345973;0.027520815842;0.035013877476;
11;276272;0.035013877476;0.042506939111;
12;694253;0.042506939111;0.050000000745;

답변 (1개)

Rik
Rik 2020년 7월 6일

0 개 추천

You will have to insert those labels with the text function.

댓글 수: 6

Thinkboard
Thinkboard 2020년 7월 7일
Any chance to annotate t automaticaally?
Rik
Rik 2020년 7월 7일
If that would be possible it would be in the documentation. So, no.
If you have trouble implementing it, feel free to post a comment with what you tried.
Now all you need are the x-values that define the center of each bar and the y values that define the height of each bar.
xCenters = h.BinEdges(1:end-1) + h.BinWidth/2;
yHeights = h.Values;
Then calculate the percentages
percentages = .... % I'll leave that up to you.
% There needs to be n values where n is the number of bars.
Convert the percentages to strings with 1 decimal place
percentageStrings = compose('%.1f',percentages*100);
Then label the peaks
text(xCenters,yHeights,percentageStrings,...
'HorizontalAlignment','center','VerticalAlignment','middle', ...
'FontSize', 8);
Hi Rik,
I've found a useful code here on data exchange.
But my main graph is using bar(X,Y) plot instead of historgram, how can i make use of percent generate from "Histogram Percentage function" to add it on my bar chart?
clear;
close all;
Array=csvread('1.csv',1,0);
Value = Array(:,2);
CStart = Array(:,3);
CEnd = Array(:,4);
edges = transpose(CStart);
figure;
subplot(2,1,1);
bar(edges,Value)
title('Histogram');
xlabel('Distnce');
ylabel('Count');
subplot(2,1,2);
histogramPercentage(Value);
title('Histogram Percentage')
xlabel('Values');
ylabel('Number of Observations');
You can either use the code Adam suggested, or look into what that function from the FEX is actually doing:
function histogramPercentage(x,fontSize)
h = histogram(x,'Visible', 'off');
[nelements,centers]= hist(x,h.NumBins);
percantages = 100 * nelements / sum(nelements);
bar(centers,nelements,'Facecolor',[0.4,0.7,0.9],'BarWidth',1);
for k = 1:numel(centers)
if percantages(k) ~= 0
text(centers(k),nelements(k),[sprintf('%.f',(percantages(k))) '%'],'HorizontalAlignment','center','VerticalAlignment','bottom','FontSize',fontSize);
end
end
So in short: you need the bin centers and the height. Then you can easily create the text objects in a loop.
Adam Danz
Adam Danz 2020년 7월 8일
Bin centers for bar chart can be computed using this appoach.

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

카테고리

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

질문:

2020년 7월 6일

댓글:

2020년 7월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by