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

24 views (last 30 days)
Lynn on 16 Oct 2019
Commented: Lynn on 16 Oct 2019
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.
LynnBob
Bozeman

Steven Lord on 16 Oct 2019
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?

#### 1 Comment

Lynn on 16 Oct 2019
Thanks, Steven,
I think this will work but I will soon be away for a couple of days before I do more with your suggestions.
By the way, I added a line to your code that standardizes the "right" y-axis curve:
% 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;
binvalues = binvalues/max(binvalues); % added by Lynn
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, '--')
This standardizes the right y-axis data, as I desire.
More later.
Thanks again.
Lynn