Search code examples
matlabmatlab-figure

Multiple vertical histograms plot in matlab


Is it possible to make multiple vertical histograms plot in Matlab into one? Much like the excel sheet enclosed ( https://drive.google.com/file/d/1H_mbyrIoln3XrnK1hLajnVNBKn13y_np/view?usp=sharing )

I want to make a plot of many vertical histogram plots into one figure, by importing excel-files, where on the y axis it has the elevation, the x axis the distance between the histogram vertical lines and the length of the histogram bars is the values in the excel sheet. The vertical height of each bar is 5.

Is this even possible? I have to put in a number of conditions for Matlab to know where to plot, but could some one show me the basic methodology?

Help would be very much appreciated!

enter image description here


Solution

  • The problem is that the parent of a Baseline object is the Axis, which prevents us from doing something like

    barh(bins1,counts1,'Basevalue',baseline1); hold on;
    barh(bins2,counts2,'Basevalue',baseline2); hold off;
    

    because the plots will automatically share the second baseline value set. There might be a workaround for this that I do not know of, so I invite anybody who knows it to show me how its done.

    For now, I was able to sort-of replicate the plot you posted a picture of in a much less elegant way. I will post code below, but before I do, I would like to argue against the use of a plot like this. Why? Because I think it is confusing, as the x-axis both relates to the plot number as well as the bin count numbers. You are in fact trying to display a 3-D data set, the three dimensions being bins, bin counts, and 'histogram number'. A plethora of methods exist for displaying 3-D data, and a series of 2-D histograms may not be the best way to go.

    That being said, here is a code that more-or-less creates the picture above, as promised. Any changes you may want to make will be more cumbersome than usual :-)

    testData = randn(10000,1);      % Generate some data
    [counts,bins] = hist(testData); % Bin the data
    
    % First histogram
    baseline1 = 0;
    p1=subplot(1,3,1); barh(bins,counts,'BaseValue',baseline1); 
    xticks(baseline1); xticklabels({0}); % Graph number on x axis at baseline (0)
    box off;          % Remove box on right side of plot
    ylabel('Property');
    
    % Second histogram
    baseline2 = max(counts)*1.2;
    sepdist = baseline2-baseline1;      % Distance that separates two baselines
    counts2 = baseline2 + counts;
    p2=subplot(1,3,2); barh(bins,counts2,'BaseValue',baseline2)
    xticks(baseline2); xticklabels({1}); % Graph number on x axis at baseline
    box off;
    Y=gca; Y.YAxis.Visible='off';
    p1p=p1.Position; p2p=p2.Position;
    p2p(1)=p1p(1)+p1p(3); p2.Position=p2p; % Move subplot so they touch
    
    % Third histogram
    baseline3 = baseline2 + sepdist;
    counts3 = baseline3+counts;
    p3=subplot(1,3,3); barh(bins,counts3,'BaseValue',baseline3)
    xticks(baseline3); xticklabels({2});
    Y=gca; Y.YAxis.Visible='off';
    box off
    p3p=p3.Position;
    p3p(1)=p2p(1)+p2p(3); p3.Position=p3p;
    
    % Add x-label when you are done:
    xl=xlabel('Test xlabel'); xl.Units='normalized';
    
    % Fiddle around with xl.Position(1) until you find a good centering:
    xl.Position(1) = -0.49;
    

    Result:

    Result