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

12 ビュー (過去 30 日間)
Lynn
Lynn 2019 年 10 月 16 日
コメント済み: Lynn 2019 年 10 月 16 日
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

採用された回答

Steven Lord
Steven Lord 2019 年 10 月 16 日
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 件のコメント
Lynn
Lynn 2019 年 10 月 16 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by