How to plot simple histogram of multiple variables?

18 views (last 30 days)
Hello, I have the following data and my aim is to plot histograms in a single plot. Please, can you give me any hint how to start? And how to put specific colours of the histograms? Thank you!
x1 = normrnd(10,1,[1,1000]) %normal distribution
x2 = gamrnd(10,1,[1,1000]) %gamma distribution
x3 = exprnd(10,[1,1000]) %exponential distribution

Accepted Answer

Image Analyst
Image Analyst on 6 Nov 2018
Edited: Image Analyst on 20 Dec 2018
How about the bar() function? Or the plot() function?
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
x1 = normrnd(10,1,[1,1000]) %normal distribution
x2 = gamrnd(10,1,[1,1000]) %gamma distribution
x3 = exprnd(10,[1,1000]) %exponential distribution
minValue = min([x1,x2,x3])
maxValue = max([x1,x2,x3])
numBins = 500; % Whatever you want.
edges = linspace(minValue, maxValue, numBins);
counts1 = histcounts(x1, edges);
counts2 = histcounts(x2, edges);
counts3 = histcounts(x3, edges);
% Plot with bars.
subplot(1, 2, 1);
bar(edges(1:end-1), counts1, 'FaceColor', 'r');
hold on;
bar(edges(1:end-1), counts2, 'FaceColor', 'g');
bar(edges(1:end-1), counts3, 'FaceColor', 'b');
grid on;
xlabel('Value', 'FontSize', 20)
ylabel('Count', 'FontSize', 20)
title('Bar Chart', 'FontSize', 20)
legend('x1 histogram', 'x2 histogram', 'x3 histogram');
% Plot with lines.
subplot(1, 2, 2);
plot(edges(1:end-1), counts1, 'r-', 'LineWidth', 2);
hold on;
plot(edges(1:end-1), counts2, 'g-', 'LineWidth', 2);
plot(edges(1:end-1), counts3, 'b-', 'LineWidth', 2);
grid on;
xlabel('Value', 'FontSize', 20)
ylabel('Count', 'FontSize', 20)
title('Line Plot', 'FontSize', 20)
legend('x1 histogram', 'x2 histogram', 'x3 histogram');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!