how to have two y plots with different scales

9 views (last 30 days)
I have the following plot and as you see, I want to do this thing that I did by hand, in matlab.
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.
Thanks

Accepted Answer

Umar
Umar on 4 Jul 2024
Edited: Walter Roberson on 24 Jun 2025
Hi Mahdi,
You mentioned that you want numbers that you 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.
Example code
% Adding numbers to the right y-axis
yyaxis right;
yticks([20 50 80]); % Define the numbers from the legend
yticklabels({'20', '50', '80'}); % Labels for the numbers
Then, you mentioned about the dots that pointed as red, to be connected to each other through a dashed line.
Example code
numbers
% Connecting specific points with a dashed line
hold on; plot(x([2, 4, 6]), y1([2, 4, 6]), 'ro--'); % Connect points 2, 4, and 6 with red dashed lines
For more information on yyaxis function, please refer to https://www.mathworks.com/help/matlab/ref/yyaxis.html#
For more information on ytick labels, please refer to https://www.mathworks.com/help/matlab/ref/yticklabels.html#
Hope this will help resolve your problems.

More Answers (2)

Umar
Umar on 4 Jul 2024

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.

  1 Comment
Mahdi
Mahdi on 4 Jul 2024
thank you for your answer but the thing is that I don't want two different plots with different y axis. I already do have the plot as you can see in the figure and I want the red dots that I showed to represent a specific number on the right y axis and as you see, the right y axis may not be scaled equally.

Sign in to comment.


David
David on 24 Jun 2025
The following just worked me me, but it took me a bit more effort than I would have liked. Its a bit of a hack, but it works.
You do need to use yyaxis like @Mahdi mentioned (or make another axes beneath your existing one with yAxisLocation set to 'right'), but don't plot anything on it. Instead, you can use a combination of linkprop and the "LimitsChangedFcn" callback in the 2nd yAxis to show the scale you want. I use a simpler case of x on left hand, and x^2 on the right.
Steps:
  1. Get your left axis and right axis with yyaxis
  2. Link the two axis (or axis rulers) so they look the same and move in a predictable way to axis interactions
  3. Define "LimitsChangedFcn" callback that doesnt change the ticks themselves, but changes only the tickLabel to the scale you want.
Your right ruler effectively becomes a function of your left ruler, where you specify whatever scale or relationship you need
Its too late to help, but maybe it will help someone else moving foward. Good luck.
axH = gca; % Your current plot axes, using whatever is method most convenient
% get your leftYAx and rightYAx
yyaxis(axH,'left');
yAxRules = get(axH,'YAxis');
%personally, I like getting the getting the rulers themselves
leftYAx = yAxRules(1);
rightYAx = yAxRules(2);
% link the properties. need at least ylimits to match. some users could
% also link the axis ticks.
lp = linkprop([leftYAx rightYAx],{'limits','color','ticklabelcolor','fontsize','fontname','scale'});
% now you have two y rulers, but theyre identical. data only is plotted once.
% now need to make your rightYAx show the scale you want.
% whenever the limits on the left ruler changes, update the ticklabels for
% the right ruler.
leftYAx.LimitsChangedFcn = @(srcRuler,limChangedEvnt) yticklabelupdate(srcRuler, limChangedEvnt) % to illustrate
function yticklabelupdate(src,evnt)
% Lets say, for example, your main axis is x, but your right axis scales as
% x^2
parentAx = src.Parent;
leftAx = parentAx.YAxis(1); % the left y ruler
rightAx = parentAx.YAxis(2); % the right y ruler
% update right y ruler as function of the scale in your left y ruler
set(rightAx, 'TickLabels', leftAx.TickValues.^2)
end

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!