Double y-axis with synchronized to each other
6 views (last 30 days)
Show older comments
Commented: Muhammad Usman Gul on 20 Jun 2021
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 on 20 Jun 2021
Is this what you want?
x = 1 : 10;
A = rand(1, 10);
B = rand(1, 10) * 8
% Convert B to percent.
Bpct = B * 100;
plot(x, Bpct, '.-', 'LineWidth', 3, 'MarkerSize', 30)
ylabel('B', 'fontSize', 20);
plot(x, A, '.-', 'LineWidth', 3, 'MarkerSize', 30)
ylabel('A', 'fontSize', 20);
More Answers (1)
dpb on 20 Jun 2021
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
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
to display the labels as percentages and they'll update automagically.
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!