MATLAB: Add line curves to histogram in image that has two y axes

histogramtwo y-axis

Is there a way that I can create a histogram that has two y axes, the left axis for the histogram and the right axis for standardized (i.e., domain 0, 1) curves (gamma, in this case) fitted to some portion of the histogram? I can "stairs" the histo data (making it a line, I presume) but following the two y-axis instructions and adding a gamma curve and its "right" y axis causes the stairs to go away (even with "hold on"). I'd prefer to retain the initial histogram and overlay it with the gammas — but could make due with the stairs. The gamma curves (nine in all) are pre-fitted and I would like to individually overlay and display them, then collectively overlay and display them on the histogram.
Thanks for your help!
LynnBob
Bozeman

Best Answer

Here's an example of creating a pair of axes, one with a histogram and one with a line.
% Sample data
x = randn(1, 1e5);
% Create the histogram on the left axes
yyaxis left
h = histogram(x);
% Make the right axes active
yyaxis right
% Get some data from the histogram
% If you already know the data you want to plot you can skip this step
binvalues = h.BinCounts;
binedges = h.BinEdges;
bincenters = (binedges(1:end-1)+binedges(2:end))/2;
% Plot the line on the right axes (since it's the active axes)
plot(bincenters, binvalues, '--')
You should see that the line on the right axes touches the histogram bars at their centers.
If you are using yyaxis and unable to show both the histogram and the other curve, can you show a small sample of your code that tries to show the other curve?