Is it possible to draw a bar plot with percentage lines?

조회 수: 7 (최근 30일)
Helena
Helena 2019년 9월 12일
편집: Adam Danz 2019년 9월 23일
Hello!
I'm drawing a bar plot with data from a file, like this. The data is sorted into bars according to the exponent of each value in scientific notation. The code is shown below.
data = sort(data);
s = floor(log10(abs(data)));
x = unique(s);
y = histc(s, x);
figure('Name', file.name);
bar(x, y);
title(file.name, 'Interpreter', 'none');
barvalues;
The plot generated is something like this:
I was looking for a way to draw percentages in the plot, something like this (the percentages are completely made up):
Do you know if this is possible and how it could be done? Thank you so much!
  댓글 수: 2
Adam Danz
Adam Danz 2019년 9월 12일
What do you mean by "draw percentages"? What do those red lines represent?
Helena
Helena 2019년 9월 12일
The red lines represent the percentage of each bar. I wanted the cumulative percentage, as shown in image. For instance, until -4 the total percentage is 1%, until -2 is %3... at 1 is 100%.

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

답변 (2개)

the cyclist
the cyclist 2019년 9월 12일
I'm not sure if you are asking for them to be automatically positioned, but there are many options for drawing lines on a plot:

Adam Danz
Adam Danz 2019년 9월 12일
편집: Adam Danz 2019년 9월 13일
Use cumsum() to compute the cumulative sum of each bar height. Then you can normalize it to the max bar height (this is how I interpreted your query but if you need something different, please add details).
Here's a functional demo
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
h = bar(y);
barcs = cumsum(h.YData);
% Normalize the cumsum to the max bar height
barcsNorm = barcs / barcs(end) .* h.YData;
% Plot lines
hold on
ph = plot([h.XData;h.XData], [zeros(size(barcsNorm)); barcsNorm], 'r-', 'LineWidth', 3);

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by