how to have two y plots with different scales

0 Comments
Accepted Answer
0 Comments
More Answers (2)
Hi Mahdi,
You asked, I want to numbers that I have mentioned in the legend section, to be placed on the right y axis of the plot with the scale that you see in the attached picture. I also want the dots that I pointed as red, to be connected to each other through a dashed line. I would appreciate any help to tell me how to do this.
To help you out, I have modify the plot properties to include a secondary y-axis and connect specific points with dashed lines. I will define two datasets, y1 and y2, based on the values of x. The, generate a figure and plot y1 against the left y-axis and y2 against the right y-axis. Afterwards, draw dashed lines connecting the points of the y1 dataset.
>> % Sample Data x = 1:10; y1 = x.^2; y2 = 2*x + 5;
% Create Plot figure; yyaxis left; plot(x, y1, 'o-r'); % Plot with red dots ylabel('Primary Y-Axis'); hold on; yyaxis right; plot(x, y2, '--b'); % Plot with dashed lines ylabel('Secondary Y-Axis'); legend('Data 1', 'Data 2');
% Connect Points with Dashed Lines for i = 1:length(x)-1 line([x(i), x(i+1)], [y1(i), y1(i+1)], 'Color', 'r', 'LineStyle', '--'); end

Hope this will help resolve your problem.
- Get your left axis and right axis with yyaxis
- Link the two axis (or axis rulers) so they look the same and move in a predictable way to axis interactions
- Define "LimitsChangedFcn" callback that doesnt change the ticks themselves, but changes only the tickLabel to the scale you want.
0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!