Double y-axis with synchronized to each other

17 ビュー (過去 30 日間)
Muhammad Usman Gul
Muhammad Usman Gul 2021 年 6 月 20 日
コメント済み: Muhammad Usman Gul 2021 年 6 月 20 日
Suppose I have two vectors A and B. Vector A has ten values from 0 to 1, and vector B has ten values from 0 to 8. The Y-axis size is different in both vectors; however, the x-axis is the same in both vectors. Currently, I can plot a dual y-axis with A on the right and B on the left axis.
When I tried to change the left y-axis from 0 to 800 (as it is a percentage) instead of 0 to 8, the right y-axis curve looks like a zero straight line on the x-axis because it has small values compared to the left y-axis.
Can anyone guide me on how I could stretch the B curve (from 0 to 800) on the left y-axis and synchronize it with the same right y-axis (0 to 1)? I want to compare the A curve with B (in percentage) curve. Hopefully, you understand my question.

採用された回答

Image Analyst
Image Analyst 2021 年 6 月 20 日
Is this what you want?
x = 1 : 10;
A = rand(1, 10);
B = rand(1, 10) * 8
% Convert B to percent.
Bpct = B * 100;
yyaxis left
plot(x, Bpct, '.-', 'LineWidth', 3, 'MarkerSize', 30)
ylabel('B', 'fontSize', 20);
ytickformat('percentage')
yyaxis right
plot(x, A, '.-', 'LineWidth', 3, 'MarkerSize', 30)
ylabel('A', 'fontSize', 20);
grid on;
  1 件のコメント
Muhammad Usman Gul
Muhammad Usman Gul 2021 年 6 月 20 日
Thank you,
I know the previous answer is also correct. But now, it has been clear to me much more. Now everything is clear. With the help of your guidance, I have obtained the plot in my work, as I requested.

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

その他の回答 (1 件)

dpb
dpb 2021 年 6 月 20 日
You can scale the percentile axis from [0 0.8] to match the [0 8] range on the other axes; if you scale to percentages, then you'll need to plot 100*Y instead of just Y to use the built in scaling/ticks on the axes.
If, for some reason, you can't (don't see why that could be, but hypothetically) use 100*Y on the plot, then you still must use the actual data range for ylim to match the data, but you can then write
yyaxis left
yticklabels(compose('%g%%',100*yticks))
to display the tick labels as percentages. If you do it this way, however, you'll need to put it into a callback function for it to be automatically updated if/when the limits on the axis are changed as this breaks the link between the tick values and tick labels.
It's far simpler to just scale the data before plotting; you can then set
ytickformat('%g%%')
to display the labels as percentages and they'll update automagically.
  1 件のコメント
Muhammad Usman Gul
Muhammad Usman Gul 2021 年 6 月 20 日
Thanks for your prompt reply.
I have already tried to multiply by 100 on the left y-axis (B vector), but the value of the right y-axis (A vector) seems too small (like zero curves). For detail, I have shared with you some figures as an example.

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

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by