Clear Filters
Clear Filters

Y double axis plot

4 views (last 30 days)
SOMNATH MAHATO
SOMNATH MAHATO on 11 Mar 2023
Edited: Star Strider on 12 Mar 2023
Below y double axis has the error of "Dimensions of matrices being concatenated are not consistent".
x = [162 132 190 470 187 157 215 495 165 135 193 473 302 272 330 610 942 912 970 1250];
y1 = [1 1.273; 0.636 0.751; 0.247 0.287; 0.267 0.32; 0.268 0.311; 0.33 0.407; 0.266 0.389; 0.768 1.22; 0.21 0.522; 0.187 0.173, 0.085 0.111; 0.031 0.038; 0.189 0.221; 0.134 0.129; 0.051 0.04; 0.0538 0.083; 0.21 0.268; 0.121 0.146; 0.058 0.072; 0.0552 0.066;];
y2 = [4271 263 110 1364 1375 10 51 6 13740 1805 229 19 932 3 3 9 1 3 1 7];
figure('Color','w')
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
xlabel ('H value(USD)')
ylabel(hAx(1),'P value (m)') % left y-axis
ylabel(hAx(2),'T value (s)') % right y-axis
grid on
Y2axis right
ax.Ylable(hAx(2)).Color = 'r';

Accepted Answer

Star Strider
Star Strider on 11 Mar 2023
Edited: Star Strider on 12 Mar 2023
The problem is:
y1 = [1 1.273; 0.636 0.751; 0.247 0.287; 0.267 0.32; 0.268 0.311; 0.33 0.407; 0.266 0.389; 0.768 1.22; 0.21 0.522; 0.187 0.173, 0.085 0.111; 0.031 0.038; 0.189 0.221; 0.134 0.129; 0.051 0.04; 0.0538 0.083; 0.21 0.268; 0.121 0.146; 0.058 0.072; 0.0552 0.066;];
↑ ← HERE
The comma should be a semicolon.
There are a few other errors.
EDIT — (12 Mar 2023 at 13:02)
I figured out how to do this with plotyy
x = [162 132 190 470 187 157 215 495 165 135 193 473 302 272 330 610 942 912 970 1250];
y1 = [1 1.273; 0.636 0.751; 0.247 0.287; 0.267 0.32; 0.268 0.311; 0.33 0.407; 0.266 0.389; 0.768 1.22; 0.21 0.522; 0.187 0.173; 0.085 0.111; 0.031 0.038; 0.189 0.221; 0.134 0.129; 0.051 0.04; 0.0538 0.083; 0.21 0.268; 0.121 0.146; 0.058 0.072; 0.0552 0.066;];
y2 = [4271 263 110 1364 1375 10 51 6 13740 1805 229 19 932 3 3 9 1 3 1 7];
[xs,idx] = sort(x);
y1s = y1(idx,:);
y2s = y2(idx);
figure('Color','w')
[hAx,hLine1,hLine2] = plotyy([NaN NaN],[0 0],xs,y2s);
hold on
scatter(hAx(1),xs,y1s(:,1), 'sr', 'MarkerFaceColor','r');
scatter(hAx(1),xs,y1s(:,2), 'ob', 'MarkerFaceColor','b');
xlabel ('H value(USD)')
ylabel(hAx(1),'P value (m)') % left y-axis
ylabel(hAx(2),'T value (s)') % right y-axis
grid on
hAx(2).YLabel.Color = 'r';
yt1 = hAx(1).YTick;
% text(1550, median(yt1), hAx(2).YLabel.String, 'Horiz','center', 'Vert','middle', 'Rotation',90, 'Color','r')
return % Stop Here - Do Not Run Code Beyond This Line
% % % EARLIER CODE —
x = [162 132 190 470 187 157 215 495 165 135 193 473 302 272 330 610 942 912 970 1250];
y1 = [1 1.273; 0.636 0.751; 0.247 0.287; 0.267 0.32; 0.268 0.311; 0.33 0.407; 0.266 0.389; 0.768 1.22; 0.21 0.522; 0.187 0.173; 0.085 0.111; 0.031 0.038; 0.189 0.221; 0.134 0.129; 0.051 0.04; 0.0538 0.083; 0.21 0.268; 0.121 0.146; 0.058 0.072; 0.0552 0.066;];
y2 = [4271 263 110 1364 1375 10 51 6 13740 1805 229 19 932 3 3 9 1 3 1 7];
figure('Color','w')
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
xlabel ('H value(USD)')
ylabel(hAx(1),'P value (m)') % left y-axis
ylabel(hAx(2),'T value (s)') % right y-axis
grid on
hAx(2).YLabel.Color = 'r';
yt1 = hAx(1).YTick;
text(1550, median(yt1), hAx(2).YLabel.String, 'Horiz','center', 'Vert','middle', 'Rotation',90, 'Color','r')
The right ‘ylabel’ isn’t showing up, although the code syntax appears to be correct. (I’ve not used plotyy since yyaxis was introduced, so I’m no longer familiar with it.)
EDIT — (11 Mar 2023 at 20:40)
The problem with the right ylabel appears to be that it is placed by default out of the figure bounds (at an x-value of 1568) because of the size of the right y-tick labels. I added a text call to position it at a point where it would appear.
.
  3 Comments
SOMNATH MAHATO
SOMNATH MAHATO on 11 Mar 2023
It should be like this
Star Strider
Star Strider on 11 Mar 2023
Edited: Star Strider on 11 Mar 2023
My pleasure!
If you have R2016a or later, use yyaxis instead.
Changing the code to use it is trivial —
x = [162 132 190 470 187 157 215 495 165 135 193 473 302 272 330 610 942 912 970 1250];
y1 = [1 1.273; 0.636 0.751; 0.247 0.287; 0.267 0.32; 0.268 0.311; 0.33 0.407; 0.266 0.389; 0.768 1.22; 0.21 0.522; 0.187 0.173; 0.085 0.111; 0.031 0.038; 0.189 0.221; 0.134 0.129; 0.051 0.04; 0.0538 0.083; 0.21 0.268; 0.121 0.146; 0.058 0.072; 0.0552 0.066;];
y2 = [4271 263 110 1364 1375 10 51 6 13740 1805 229 19 932 3 3 9 1 3 1 7];
figure('Color','w')
yyaxis left
scatter(x,y1(:,1), 'sr', 'MarkerFaceColor','r');
hold on
scatter(x,y1(:,2), 'ob', 'MarkerFaceColor','b');
hold off
ylabel('P value (m)') % left y-axis
yyaxis right
plot(x, y2)
ylabel('T value (s)', 'Color','r') % right y-axis
grid on
xlabel ('H value(USD)')
However for whatever reason, plotyy is not showing the right ylabel. The code appears to be correct, and I can see the right ylabel is set correctly with:
hAx(2).YLabel.String
however it is not being shown on the plot.
EDIT — (11 Mar 2023 at 20:45)
See my EDIT to my Answer where I determined the reason it was not appearing, and provided a work-around for it.
EDIT — (11 Mar 2023 at 21:16)
I just now saw your second Comment.
It is not possible to do a scatter plot with plotyy (I experimented with that without success, even using the hold function), however it is possible with yyaxis, as I show here. I am using your data as presented, rather than sorting them. If you want to sort them. do this:
[xs,idx] = sort(x);
y1s = y1(idx,:);
y2s = y2(idx);
Then plot the sorted values —
x = [162 132 190 470 187 157 215 495 165 135 193 473 302 272 330 610 942 912 970 1250];
y1 = [1 1.273; 0.636 0.751; 0.247 0.287; 0.267 0.32; 0.268 0.311; 0.33 0.407; 0.266 0.389; 0.768 1.22; 0.21 0.522; 0.187 0.173; 0.085 0.111; 0.031 0.038; 0.189 0.221; 0.134 0.129; 0.051 0.04; 0.0538 0.083; 0.21 0.268; 0.121 0.146; 0.058 0.072; 0.0552 0.066;];
y2 = [4271 263 110 1364 1375 10 51 6 13740 1805 229 19 932 3 3 9 1 3 1 7];
[xs,idx] = sort(x);
y1s = y1(idx,:);
y2s = y2(idx);
figure('Color','w')
yyaxis left
scatter(xs,y1s(:,1), 'sr', 'MarkerFaceColor','r');
hold on
scatter(xs,y1s(:,2), 'ob', 'MarkerFaceColor','b');
hold off
ylabel('P value (m)') % left y-axis
yyaxis right
plot(xs, y2s)
ylabel('T value (s)', 'Color','r') % right y-axis
grid on
xlabel ('H value(USD)')
.
.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!