Is there anyway to create a plot with a discontinued x-axis, where the one set of data is linked to the left y-axis, and a second data is linked to right y-axis?
18 次查看(过去 30 天)
显示 更早的评论
Hello,
I would like to plot 2 sets of data on the same x-axis, but the x-axis is discontinued. In the first range, R1, the x-axis should cover 0-10. In the second range, R2, the x-axis should cover 40-50. In both ranges I would like to plot y1 on the left axis, and y2 on the right axis. I used the example for Plotting Data on a Discontinuous x-Axis as a template. I can plot a discontinued x-axis with y1 in both ranges linked to the left y-axis. Please see my code below:
x_R1 = 0:10;
y1_R1 = rand(1,11)*10;
y2_R1 = rand(1,11)*100;
x_R2 = 40:50;
y1_R2 = rand(1,11)*10;
y2_R2 = rand(1,11)*100;
figure
t = tiledlayout(1,2,'TileSpacing','tight');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [1 2];
ax1 = axes(t);
plot(ax1,x_R1,y1_R1, 'b')
xline(ax1,10,':');
ax1.Box = 'off';
xlim(ax1,[0 10])
ax2 = axes(t);
ax2.Layout.Tile = 2;
plot(ax2,x_R2,y1_R2, 'b')
xline(ax2,40,':');
ax2.YAxis(1).Visible = 'off';
ax2.Box = 'off';
xlim(ax2,[40 50])
linkaxes([ax1 ax2], 'y')
But, when I add the second set of data, y2, using yyaxis right, the y1 data loses the link to the left y-axis. Please see below:
x_R1 = 0:10;
y1_R1 = rand(1,11)*10;
y2_R1 = rand(1,11)*100;
x_R2 = 40:50;
y1_R2 = rand(1,11)*10;
y2_R2 = rand(1,11)*100;
figure
t = tiledlayout(1,2,'TileSpacing','tight');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [1 2];
ax1 = axes(t);
ax1.Layout.Tile = 1;
plot(ax1,x_R1,y1_R1, 'b')
%Add right y-axis data to R1
yyaxis right
plot(ax1, x_R1,y2_R1, 'r');
%
ax1.YAxis(2).Visible = 'off';
xline(ax1,10,':');
ax1.Box = 'off';
xlim(ax1,[0 10])
ax2 = axes(t);
ax2.Layout.Tile = 2;
plot(ax2,x_R2,y1_R2, 'b')
%Add right y-axis data to R2
yyaxis right
plot(ax2, x_R2,y2_R2, 'r');
%
xline(ax2,40,':');
ax2.YAxis(1).Visible = 'off';
ax2.Box = 'off';
xlim(ax2,[40 50])
linkaxes([ax1 ax2], 'y')
Is there anyway to create this discontinued x-axis plot, where the y1 data is linked to the left y-axis, and the y2 data is linked to right y-axis?
Thank you!
5 个评论
Joe Vinciguerra
2023-3-27
Here's an alternative approach:
x_R1 = 0:10;
y1_R1 = 0:10;
y2_R1 = 100:-1:90;
x_R2 = 40:50;
y1_R2 = 10:20;
y2_R2 = 90:-1:80;
t = tiledlayout(1, 2);
yyaxis(nexttile(1), 'left')
plot(nexttile(1),x_R1,y1_R1, 'b')
yyaxis(nexttile(1), 'right')
plot(nexttile(1), x_R1,y2_R1, 'r');
yyaxis(nexttile(2), 'left')
plot(nexttile(2),x_R2,y1_R2, 'b')
yyaxis(nexttile(2), 'right')
plot(nexttile(2), x_R2,y2_R2, 'r');
YLmin = min([nexttile(1).YAxis(1).Limits, nexttile(2).YAxis(1).Limits]);
YLmax = max([nexttile(1).YAxis(1).Limits, nexttile(2).YAxis(1).Limits]);
YRmin = min([nexttile(1).YAxis(2).Limits, nexttile(2).YAxis(2).Limits]);
YRmax = max([nexttile(1).YAxis(2).Limits, nexttile(2).YAxis(2).Limits]);
yyaxis(nexttile(1), 'left')
ylim([YLmin YLmax])
yyaxis(nexttile(1), 'right')
ylim([YRmin YRmax])
yyaxis(nexttile(2), 'left')
ylim([YLmin YLmax])
yyaxis(nexttile(2), 'right')
ylim([YRmin YRmax])
Or with your original code:
x_R1 = 0:10;
y1_R1 = 0:10;
y2_R1 = 100:-1:90;
x_R2 = 40:50;
y1_R2 = 10:20;
y2_R2 = 90:-1:80;
figure
t = tiledlayout(1,2,'TileSpacing','tight');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [1 2];
ax1 = axes(t);
plot(ax1,x_R1,y1_R1, 'b')
yyaxis right
plot(ax1, x_R1,y2_R1, 'r');
ax1.YAxis(2).Visible = 'off';
xline(ax1,10,':');
ax1.Box = 'off';
xlim(ax1,[0 10])
ax2 = axes(t);
ax2.Layout.Tile = 2;
plot(ax2,x_R2,y1_R2, 'b')
yyaxis right
plot(ax2, x_R2,y2_R2, 'r');
xline(ax2,40,':');
ax2.YAxis(1).Visible = 'off';
ax2.Box = 'off';
xlim(ax2,[40 50])
linkaxes([ax1 ax2], 'y')
YLmin = min([ax1.YAxis(1).Limits, ax2.YAxis(1).Limits]);
YLmax = max([ax1.YAxis(1).Limits, ax2.YAxis(1).Limits]);
YRmin = min([ax1.YAxis(2).Limits, ax2.YAxis(2).Limits]);
YRmax = max([ax1.YAxis(2).Limits, ax2.YAxis(2).Limits]);
yyaxis(ax1, 'left')
ylim([YLmin YLmax])
yyaxis(ax1, 'right')
ylim([YRmin YRmax])
yyaxis(ax2, 'left')
ylim([YLmin YLmax])
yyaxis(ax2, 'right')
ylim([YRmin YRmax])
采纳的回答
Joe Vinciguerra
2023-3-28
Glad I was able to help in the comments. Please accept this answer for future generations.
From above comments:
x_R1 = 0:10;
y1_R1 = 0:10;
y2_R1 = 100:-1:90;
x_R2 = 40:50;
y1_R2 = 10:20;
y2_R2 = 90:-1:80;
t = tiledlayout(1, 2);
yyaxis(nexttile(1), 'left')
plot(nexttile(1),x_R1,y1_R1, 'b')
yyaxis(nexttile(1), 'right')
plot(nexttile(1), x_R1,y2_R1, 'r');
yyaxis(nexttile(2), 'left')
plot(nexttile(2),x_R2,y1_R2, 'b')
yyaxis(nexttile(2), 'right')
plot(nexttile(2), x_R2,y2_R2, 'r');
YLmin = min([nexttile(1).YAxis(1).Limits, nexttile(2).YAxis(1).Limits]);
YLmax = max([nexttile(1).YAxis(1).Limits, nexttile(2).YAxis(1).Limits]);
YRmin = min([nexttile(1).YAxis(2).Limits, nexttile(2).YAxis(2).Limits]);
YRmax = max([nexttile(1).YAxis(2).Limits, nexttile(2).YAxis(2).Limits]);
yyaxis(nexttile(1), 'left')
ylim([YLmin YLmax])
yyaxis(nexttile(1), 'right')
ylim([YRmin YRmax])
yyaxis(nexttile(2), 'left')
ylim([YLmin YLmax])
yyaxis(nexttile(2), 'right')
ylim([YRmin YRmax])
Or with your original code:
x_R1 = 0:10;
y1_R1 = 0:10;
y2_R1 = 100:-1:90;
x_R2 = 40:50;
y1_R2 = 10:20;
y2_R2 = 90:-1:80;
figure
t = tiledlayout(1,2,'TileSpacing','tight');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [1 2];
ax1 = axes(t);
plot(ax1,x_R1,y1_R1, 'b')
yyaxis right
plot(ax1, x_R1,y2_R1, 'r');
ax1.YAxis(2).Visible = 'off';
xline(ax1,10,':');
ax1.Box = 'off';
xlim(ax1,[0 10])
ax2 = axes(t);
ax2.Layout.Tile = 2;
plot(ax2,x_R2,y1_R2, 'b')
yyaxis right
plot(ax2, x_R2,y2_R2, 'r');
xline(ax2,40,':');
ax2.YAxis(1).Visible = 'off';
ax2.Box = 'off';
xlim(ax2,[40 50])
linkaxes([ax1 ax2], 'y')
YLmin = min([ax1.YAxis(1).Limits, ax2.YAxis(1).Limits]);
YLmax = max([ax1.YAxis(1).Limits, ax2.YAxis(1).Limits]);
YRmin = min([ax1.YAxis(2).Limits, ax2.YAxis(2).Limits]);
YRmax = max([ax1.YAxis(2).Limits, ax2.YAxis(2).Limits]);
yyaxis(ax1, 'left')
ylim([YLmin YLmax])
yyaxis(ax1, 'right')
ylim([YRmin YRmax])
yyaxis(ax2, 'left')
ylim([YLmin YLmax])
yyaxis(ax2, 'right')
ylim([YRmin YRmax])
0 个评论
更多回答(1 个)
Kshittiz
2023-3-28
Hello Antonio,
I understand that you are trying to plot 2 sets of data on the same x-axis, but the x-axis was discontinued. There were 2 ranges, R1 and R2 respectively, and you were trying to plot y1 on the left axis and y2 on the right axis for both the ranges, but you were unable to do.
To achieve this, you have to calculate minimum and maximum limits for both y1 and y2(which are YLMin and YLMax for y1 and YRMin and YRMax for y2 in his code). And then, just use the "yyaxis" function, to create a chart with two y axes and then add all 4 limits which were calculated to them.
Below is the code for the same:
YLmin = min([nexttile(1).YAxis(1).Limits, nexttile(2).YAxis(1).Limits]);
YLmax = max([nexttile(1).YAxis(1).Limits, nexttile(2).YAxis(1).Limits]);
YRmin = min([nexttile(1).YAxis(2).Limits, nexttile(2).YAxis(2).Limits]);
YRmax = max([nexttile(1).YAxis(2).Limits, nexttile(2).YAxis(2).Limits]);
yyaxis(nexttile(1), 'left')
ylim([YLmin YLmax])
yyaxis(nexttile(1), 'right')
ylim([YRmin YRmax])
yyaxis(nexttile(2), 'left')
ylim([YLmin YLmax])
yyaxis(nexttile(2), 'right')
ylim([YRmin YRmax])
I hope it helps!
Thanks!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!