|
| 1 | +function varargout = histyy(data1,nBins1,data2,nBins2,varargin) |
| 2 | +%% function to plot two histograms on the same plot on two different axes. |
| 3 | +% Syntax |
| 4 | +% ------ |
| 5 | +% histyy(data1, nBins1, data2, nBins2) |
| 6 | +% histyy(data1, nBins1, data2, nBins2) |
| 7 | +% histyy(data1, nBins1, data2, nBins2, label1, label2) |
| 8 | +% hAx = histyy(...) |
| 9 | +% |
| 10 | +% Inputs |
| 11 | +% ------ |
| 12 | +% data1 - vector containing first histogram |
| 13 | +% nBins1 - number of bins in first histogram |
| 14 | +% data2 - vector containing second histogram |
| 15 | +% nBins2 - number of bins in second histogram |
| 16 | +% label1 - ylabel for first histogram |
| 17 | +% label2 - ylabel for second histogram |
| 18 | +% |
| 19 | +% Outputs |
| 20 | +% ------- |
| 21 | +% hAx - vector containing handles to axes |
| 22 | +% hAx(1) contains handle to first histogram axis |
| 23 | +% hAx(2) contains handle to second histogram axis |
| 24 | +% |
| 25 | +% Author : araja 11/07/11 |
| 26 | + |
| 27 | +if isempty(varargin) |
| 28 | +label1 = ''; |
| 29 | +label2 = ''; |
| 30 | +elseif length(varargin) ~= 2 |
| 31 | +error('Provide both label strings.'); |
| 32 | +else |
| 33 | +label1 = varargin{1}; |
| 34 | +label2 = varargin{2}; |
| 35 | +end |
| 36 | + |
| 37 | +if isempty(nBins1) |
| 38 | +nBins1 = 10; |
| 39 | +end |
| 40 | +if isempty(nBins2) |
| 41 | +nBins2 = 10; |
| 42 | +end |
| 43 | + |
| 44 | +% create one axis or get current axes |
| 45 | +hAx1 = gca; |
| 46 | + |
| 47 | +% get position of axis |
| 48 | +posAx1 = get(hAx1, 'Position'); |
| 49 | + |
| 50 | +% create an overlapping axis at the same location |
| 51 | +hAx2 = axes('Position', posAx1); |
| 52 | + |
| 53 | +% histogram for first data vector |
| 54 | +hist(hAx1,data1,nBins1); |
| 55 | +set(findobj(hAx1,'Type','patch'),'FaceColor','b','EdgeColor','w', 'facealpha',0.75); |
| 56 | + |
| 57 | + |
| 58 | +% histogram for second data vector |
| 59 | +hist(hAx2, data2,nBins2); |
| 60 | + |
| 61 | +% make second axis transparent |
| 62 | +set(hAx2,'Color','none'); |
| 63 | + |
| 64 | +% change color of second histogram |
| 65 | +set(findobj(hAx2,'Type','patch'),'FaceColor','g','EdgeColor','w', 'facealpha',0.75); |
| 66 | + |
| 67 | +% ylabel for histogram 1 |
| 68 | +ylabel(hAx1,label1); |
| 69 | + |
| 70 | +% ylabel for histogram 2 |
| 71 | +set(hAx2,'YAxisLocation','right'); |
| 72 | +ylabel(hAx2,label2); |
| 73 | + |
| 74 | +% set x-axis limits |
| 75 | +lim1 = get(hAx1, 'XLim'); |
| 76 | +lim2 = get(hAx2, 'XLim'); |
| 77 | + |
| 78 | +set(hAx1, 'XLim', [min([lim1(1) lim2(1)]) max([lim1(2) lim2(2)])]); |
| 79 | +set(hAx2, 'XLim', [min([lim1(1) lim2(1)]) max([lim1(2) lim2(2)])]); |
| 80 | +set(hAx2, 'XTickLabel',[]) |
| 81 | + |
| 82 | +% output |
| 83 | +if nargout == 1 |
| 84 | +varargout{1} = [hAx1 hAx2]; |
| 85 | +end |
| 86 | + |
| 87 | + |
0 commit comments