How do i create a multi-colour histogram using excel data?

3 vues (au cours des 30 derniers jours)
ka chun yick
ka chun yick le 3 Fév 2020
Commenté : J. Alex Lee le 12 Fév 2020
Hi, I tried to make a histogram same as the picture below. The yA, yB, yC are the data which i exported from excel. However, i got the error message at the end:
1) Error using horzcat
Dimensions of arrays being concatenated are not consistent.
2) Error in Untitled (line 20)
yA2 = [yA minY maxY];
What have i done wrong?
clf
clear all
clc
% x = x values for yA and yB (not needed for histogram)
% yA = data set A
% yB = data set B
% nBins = number of bins to use in histograms
% yA=30mins, yB=120mins, yC=240mins
Load yA
Load yB
Load yC
% Set extrema
minY = 0; %there are no values lower than this
maxY = 50; %there are no values higher than this
% Add in extrema placeholders to adjust bins to a common scale
yA2 = [yA minY maxY];
yB2 = [yB minY maxY];
yC2 = [yC minY maxY];
% Bin data
[countsA2, binsA2] = hist(yA2, nBins);
[countsB2, binsB2] = hist(yB2, nBins);
[countsC2, binsC2] = hist(yC2, nBins);
% Remove extrema placeholders from counts
histEnds = zeros(size(binsA2));
%data sets A and B have the same nBins, so binsA2 and binsB2 are same length
histEnds(1) = 1; %removes minimum placeholder
histEnds(end) = 1; %removes maximum placeholder
countsA3 = countsA2 - histEnds;
countsB3 = countsB2 - histEnds;
countsC3 = countsC2 - histEnds;
% Plot histograms
hold all
bar(binsA2, countsA3, 'b')
bar(binsB2, countsB3, 'r')
bar(binsC2, countsC3, 'g')
hold off
% Labels
set(gca, 'XLim', [minY maxY])
xlabel('y value')
ylabel('counts')
legend({'A', 'B', 'C'})

Réponses (4)

J. Alex Lee
J. Alex Lee le 3 Fév 2020
Modifié(e) : J. Alex Lee le 12 Fév 2020
I suspect your yA, yB, yC are column vectors, which you are trying to horizontally concatenate with scalars. You can probably fix this error by
yA2 = [yA;minY;minX]
But, I think you are making your script too complicated; don't alter you data to fit end visualization goals, but rather look for options in your tools to do that.
% Load yA
% Load yB
% Load yC
% comment out below 3 lines and uncomment your original data loads above
yA = randn(1000,1)*7+15;
yB = randn(1000,1)*3+7;
yC = randn(1000,1)*4+30;
% specify number of bins and edges of those bins; this example evenly spaces bins
NumBins = 25;
BinEdges = linspace(0,50,25);
% use histcounts and specify your bins
cntA = histcounts(yA,'BinEdges',BinEdges);
cntB = histcounts(yB,'BinEdges',BinEdges);
cntC = histcounts(yC,'BinEdges',BinEdges);
% plot
figure(1); cla; hold on;
% convert bin edges into bin centers
b = BinEdges(1:end-1)+diff(BinEdges)/2
% use bar
bar(b,[cntA',cntB',cntC'],'stacked')
  1 commentaire
J. Alex Lee
J. Alex Lee le 12 Fév 2020
Edited first answer to correct some bugs.

Connectez-vous pour commenter.


ka chun yick
ka chun yick le 6 Fév 2020
Hi,
This is my updated script. But it still got some problems at the end:
Undefined function or variable 'nbins'.
Error in Untitled (line 20)
[countsA2, binsA2] = hist(yA2,nbins);
clf
clear all
clc
% x = x values for yA and yB (not needed for histogram)
% yA = data set A
% yB = data set B
% nBins = number of bins to use in histograms
% yA=30mins, yB=120mins, yC=240mins
load yA
load yB
load yC
% Set extrema
minY = 0; %there are no values lower than this
maxY = 50; %there are no values higher than this
% Add in extrema placeholders to adjust bins to a common scale
yA2 = [yA;minY;maxY];
yB2 = [yB;minY;maxY];
yC2 = [yC;minY;maxY];
% Bin data
[countsA2, binsA2] = hist(yA2,nbins);
[countsB2, binsB2] = hist(yB2,nbins);
[countsC2, binsC2] = hist(yC2,nbins);
% Remove extrema placeholders from counts
histEnds = zeros(size(binsA2));
%data sets A and B have the same nBins, so binsA2 and binsB2 are same length
histEnds(1) = 1; %removes minimum placeholder
histEnds(end) = 1; %removes maximum placeholder
countsA3 = countsA2 - histEnds;
countsB3 = countsB2 - histEnds;
countsC3 = countsC2 - histEnds;
% Plot histograms
hold all
bar(binsA2, countsA3, 'b')
bar(binsB2, countsB3, 'r')
bar(binsC2, countsC3, 'g')
hold off
% Labels
set(gca, 'XLim', [minY maxY])
xlabel('y value')
ylabel('counts')
legend({'A', 'B', 'C'})
  1 commentaire
J. Alex Lee
J. Alex Lee le 6 Fév 2020
Modifié(e) : J. Alex Lee le 6 Fév 2020
The error sounds straightforward...you have not defined nBins.
Does my solution not work?

Connectez-vous pour commenter.


ka chun yick
ka chun yick le 7 Fév 2020
Hi,
I still got a problem on the line 20, bar(b,[cA',cB'],'stacked'), any thought? Capture - co.png
  1 commentaire
J. Alex Lee
J. Alex Lee le 7 Fév 2020
Modifié(e) : J. Alex Lee le 7 Fév 2020
Yea, my bad. I was incomplete in my last line. Here's a tested example with test data.
% Load yA
% Load yB
% Load yC
% comment out below 3 lines and uncomment your original data loads above
yA = randn(1000,1)*7+15;
yB = randn(1000,1)*3+7;
yC = randn(1000,1)*4+30;
% specify number of bins and edges of those bins; this example evenly spaces bins
NumBins = 25;
BinEdges = linspace(0,50,25);
% use histcounts and specify your bins
cntA = histcounts(yA,'BinEdges',BinEdges);
cntB = histcounts(yB,'BinEdges',BinEdges);
cntC = histcounts(yC,'BinEdges',BinEdges);
% plot
figure(1); cla; hold on;
% convert bin edges into bin centers
b = BinEdges(1:end-1)+diff(BinEdges)/2
% use bar
bar(b,[cntA',cntB',cntC'],'stacked')
untitled.png
But note that the error message told you exactly what was wrong.

Connectez-vous pour commenter.


ka chun yick
ka chun yick le 11 Fév 2020
Thanks, it works. In additional, i would like to add a distribution fit. I looked at the tutorial: https://uk.mathworks.com/help/stats/histfit.html
histfit(data) plots a histogram of values in data using the number of bins equal to the square root of the number of elements in data and fits a normal density function.
So, hisfit(data) will do its work. How do i add this line to the script?
% Load yA
% Load yB
% Load yC
% comment out below 3 lines and uncomment your original data loads above
yA = randn(1000,1)*7+15;
yB = randn(1000,1)*3+7;
yC = randn(1000,1)*4+30;
% specify number of bins and edges of those bins; this example evenly spaces bins
NumBins = 25;
BinEdges = linspace(0,50,25);
% use histcounts and specify your bins
cntA = histcounts(yA,'BinEdges',BinEdges);
cntB = histcounts(yB,'BinEdges',BinEdges);
cntC = histcounts(yC,'BinEdges',BinEdges);
% plot
figure(1); cla; hold on;
% convert bin edges into bin centers
b = BinEdges(1:end-1)+diff(BinEdges)/2
% use bar
bar(b,[cntA',cntB',cntC'],'stacked')
  2 commentaires
J. Alex Lee
J. Alex Lee le 11 Fév 2020
I don't have the toolbox. It seems you already have the full documentation, why not use it?
ka chun yick
ka chun yick le 11 Fév 2020
Hi,
I am using the academic version so i don't have the access of toolbox. Any idea that i can add the histfit()to the existing sctipt? I am not sure which line should i define....

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by