Histogram with two axes

33 ビュー (過去 30 日間)
Felix Müller
Felix Müller 2021 年 11 月 22 日
コメント済み: Felix Müller 2021 年 11 月 23 日
I want to add an axis to my histogram plot, I want the left one to be the absolute axis and the right one to be relative. I know how to plot those individually:
h = histogram(data);
h = histogram(data, 'Normalization', 'probability');
But I want to combine them. All approaches I found for using two axes assume I want two different things plotted, but I want one plot and only add the corresponding axis.

採用された回答

Benjamin Kraus
Benjamin Kraus 2021 年 11 月 22 日
If I understand correctly, you want two y-axes: one with the real numbers and another with relative values.
There is no built-in way to do this, but it can be done with the yyaxis command, and some manual code to synchronize the axes. Note that using this approach, it is up to you to make sure the left and right say in-sync with each other. You do this by setting the limits on the right based on the limits on the left.
data = randn(1000,1);
yyaxis left
h = histogram(data);
leftLim = ylim; % Query the left limits
yyaxis right
scaleFactor = numel(data);
ylim(leftLim/scaleFactor); % Set the right limits
  2 件のコメント
Benjamin Kraus
Benjamin Kraus 2021 年 11 月 22 日
A more advanced approach is to use the LimitsChangedFcn to keep the two rulers in sync.
This will allow you to pan/zoom on the axes and keep the rulers aligned properly.
Note in the code below the YAxis property on the Axes is a vector with two elements: the first is a handle to the left y-axis and the second is a handle to the right y-axis.
data = randn(1000,1);
ax = axes;
yyaxis left
h = histogram(data);
scaleFactor = numel(data);
ax.YAxis(1).LimitsChangedFcn = @(~,e) set(ax.YAxis(2),'Limits', e.NewLimits/scaleFactor);
Felix Müller
Felix Müller 2021 年 11 月 23 日
Thanks a lot! For me the first approach is enough because I only produce and save the pictures and don't scroll them, but thanks for the advanced approach as well!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by