Изменить цвет фона гистограммы в процентах - PullRequest
1 голос
/ 17 мая 2019

У меня есть гистограмма, и я хотел бы раскрасить фон между 16,5% и 83,5% точек, которые распределены в гистограмме.

Как я могу это сделать? Как найти эти точки?

Данные в file - один столбец значений.

h = histogram( file, 50 );

Ответы [ 2 ]

6 голосов
/ 17 мая 2019

просто, как добавить еще один вариант.Используя свойства гистограммы и prctile, чтобы найти пределы:

data = randn(100000,1);

% Start with the original histogram
figure;
h=histogram(data,50);
% Find the bin edges you received.
be=h.BinEdges;

% Find the limits where your percentile limits lie
y=prctile(data,[16.5 83.5]);

% However, percentile limits will not generally concide with your bin-limits, so this must be fudged.

% Option A: Adjust be, to lie on the percentiles.
% DYI

% Option B: Adjust your limits for a pretty plot

% Find which be indicies are closest to the desired limits.
vals=y(:); 
rv=be(:)';
diffs=bsxfun(@minus,vals, rv); % Finds differences to all be for all vals.
[~,inds]=min(abs(diffs),[],2); % Finds the minimum ones.
vals=rv(inds);                 % Find values to use for the cutoff.

% Replace the original plot with the inner cut.
h1=histogram(data(data>vals(1) & data<vals(2)),'BinEdges',be);
hold on;
% Plot the data outside the limits.
h2=histogram(data(data<vals(1) | data>vals(2)),'BinEdges',be);

% Pretty colors have ensued. As per post, you can color the tails to
% something else
h2.FaceColor='white';

Colored Histogram

Причитается Тому R за округление до определенных значений: https://se.mathworks.com/matlabcentral/fileexchange/37674-roundtowardvec

5 голосов
/ 17 мая 2019

Подробности см. В комментариях к коду, в основном вы можете использовать patch, чтобы выделить фон, и некоторую логическую индексацию, чтобы найти, какие ячейки находятся в пределах вашего порога 16,5% - 83,5%.

Здесь используются bar и histcounts для создания гистограммы, а не histogram, так как вы получаете более полезные выходные данные и нам нужны промежуточные шаги перед построением графика.

rng(0); % for repeatable random numbers
x = normrnd( 0, 1, 1000, 1 ) * 10; % Create data

% Get the histogram counts with 50 bins
[hc, edges] = histcounts( x, 50 );

% Lower and upper bounds we're interested in highlighting
region = [0.165, 0.835];
% Cumulative percentage across the bins
pct = cumsum( hc ) / sum( hc );
% Index to get which meet our bounds
idx = pct >= region(1) & pct <= region(2);

% Set up the plot
x = (edges(1:end-1)+edges(2:end))/2;
maxY = 1.1*max(hc);
n = nnz(idx);

% Plot
figure; hold on
patch( [x(idx),fliplr(x(idx))], [zeros(1,n),ones(1,n)]*maxY, 'y', 'edgecolor', 'none' );
bar( x, hc );
hold off
ylim( [0, maxY] );

Результат:

hist

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...