How do i create a multi-colour histogram using excel data?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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'})
0 commentaires
Réponses (4)
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
ka chun yick
le 6 Fév 2020
1 commentaire
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?
ka chun yick
le 7 Fév 2020
1 commentaire
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')
But note that the error message told you exactly what was wrong.
ka chun yick
le 11 Fév 2020
2 commentaires
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?
Voir également
Catégories
En savoir plus sur Data Distribution Plots dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!