Is it possible to plot a histogram of several columns in Matlab using a histogram () instead of hist ()

Using the function hist(), you can build this:[link] (http://nl.mathworks.com/help/examples/graphics/CreateHistogramBarPlotwithMatrixInputExample_01.png).

Matlab recommends using histogram()instead hist().

The construction of several histograms in the same graph with the help histogram()leads to the following:[link] (http://nl.mathworks.com/help/examples/matlab/PlotMultipleHistogramsExample_01.png)

Columns overlap and not side by side.

Is it possible to plot a histogram with columns side by side in the same graph using a function histogram()? If so, how do I do this?

Code snippet:

a = randn(100, 2);

edges = -3:3;
xbins = edges(1:end-1)+0.5;

figure(1)
hist(a, xbins)

figure(2), hold on
histogram(a(:, 1), edges)
histogram(a(:, 2), edges)
+4
source share
2 answers

How about this?

Click here to get an image of the plot.

data1 = randn(1000,1);
data2 = randn(1000,1);
data2 = data2 - 1.5*ones(size(data2));

lowest_boundary = min(min(data1), min(data2));
highest_boundary = max(max(data1), max(data2));
nbins = 10;

boundaries = linspace(lowest_boundary, highest_boundary, nbins + 1);

bin_assighnments1 = discretize(data1, boundaries);
bin_assighnments2 = discretize(data2, boundaries);

bin_counts1 = zeros(numel(boundaries) - 1, 1);
bin_counts2 = zeros(numel(boundaries) - 1, 1);

for m = 1:numel(bin_assighnments1)
    n = bin_assighnments1(m);
    bin_counts1(n) = 1 + bin_counts1(n);

    n = bin_assighnments2(m);
    bin_counts2(n) = 1 + bin_counts2(n);
end

merged_bin_counts = cat(2, bin_counts1, bin_counts2);

x = zeros(1, nbins);

for m = 1:nbins
    x(m) = (boundaries(m) + boundaries(m+1))/2;
end

bar(x, merged_bin_counts);
+1

?

x1 = randn(100,1);
x2 = randn(100,1);

figure_rows = 1;
figure_cols = 2;

figure
subplot(figure_rows, figure_cols, 1);
histogram(x1);
title('Hist One')

subplot(figure_rows, figure_cols, 2);
histogram(x2);
title('Hist Two')       
-1

Source: https://habr.com/ru/post/1649872/


All Articles