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

1 Ansicht (letzte 30 Tage)
Lynn
Lynn am 16 Okt. 2019
Kommentiert: Lynn am 16 Okt. 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.
Thanks for your help!
LynnBob
Bozeman

Akzeptierte Antwort

Steven Lord
Steven Lord am 16 Okt. 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 Kommentar
Lynn
Lynn am 16 Okt. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by