MATLAB: Is there a way to add multiple histograms to the same plot, similar to PLOTYY in MATLAB 7.13 (R2011b)

histogramshistyyMATLABmultiple

I would like to place 2 histograms in the same picture like PLOTYY. How do I do this?

Best Answer

There is no direct functionality for creating two histograms on the same plot. In order to create two histograms on the same plot, similar to the PLOTYY function, you need to create overlapping axes and then plot each histogram on one axis.
1.In order to create overlapping axes, simply create one axes and then create another axes with the same position as the first.
2.Plot each histogram on one each of the two axes.
3.Make the second axes transparent by setting the ‘Color’ property to none.
4.After plotting each histogram on each axes, set the ‘YAxisLocation’ property of the second histogram to ‘Right’, so that the Y-axis for that histogram is on the right.
5.Then make sure the X-axis limits for both plots are the same using the ‘XLim’ property. Adjust the limits based on the limits of each histogram plot.
6.Set the color of the second histogram as needed.
The following example function HISTYY demonstrates how to do this:
function varargout = histyy(data1,nBins1,data2,nBins2,varargin)
%%function to plot two histograms on the same plot on two different axes.
% Syntax
% ------

% histyy(data1, nBins1, data2, nBins2)

% histyy(data1, nBins1, data2, nBins2)
% histyy(data1, nBins1, data2, nBins2, label1, label2)
% hAx = histyy(...)
%


% Inputs
% ------
% data1 - vector containing first histogram
% nBins1 - number of bins in first histogram
% data2 - vector containing second histogram
% nBins2 - number of bins in second histogram
% label1 - ylabel for first histogram
% label2 - ylabel for second histogram
%
% Outputs
% -------
% hAx - vector containing handles to axes
% hAx(1) contains handle to first histogram axis
% hAx(2) contains handle to second histogram axis
%
% Author : araja 11/07/11
if isempty(varargin)
label1 = '';
label2 = '';
elseif length(varargin) ~= 2
error('Provide both label strings.');
else
label1 = varargin{1};
label2 = varargin{2};
end
if isempty(nBins1)
nBins1 = 10;
end
if isempty(nBins2)
nBins2 = 10;
end
% create one axis or get current axes
hAx1 = gca;
% get position of axis
posAx1 = get(hAx1, 'Position');
% create an overlapping axis at the same location
hAx2 = axes('Position', posAx1);
% histogram for first data vector
hist(hAx1,data1,nBins1);
% histogram for second data vector
hist(hAx2, data2,nBins2);
% make second axis transparent
set(hAx2,'Color','none');
% change color of second histogram
set(findobj(hAx2,'Type','patch'),'FaceColor','r','EdgeColor','r');
% ylabel for histogram 1
ylabel(hAx1,label1);
% ylabel for histogram 2
set(hAx2,'YAxisLocation','right');
ylabel(hAx2,label2);
% set x-axis limits
lim1 = get(hAx1, 'XLim');
lim2 = get(hAx2, 'XLim');
set(hAx1, 'XLim', [min([lim1(1) lim2(1)]) max([lim1(2) lim2(2)])]);
set(hAx2, 'XLim', [min([lim1(1) lim2(1)]) max([lim1(2) lim2(2)])]);
set(hAx2, 'XTickLabel',[])
% output
if nargout == 1
varargout{1} = [hAx1 hAx2];
end
For help on usage of this function, type:
>> help histyy