How to plot data with two different X- axis in a single plot?

Hello all,
I am trying to plot a data of multiple plots in a single graph. I am facing a problem in plotting it. I need the data from different Y axis in a single axis bar i.e, im my case it is from -200:200. And for X axis I need a scale for each and every X axis data as the data varies for every Y axis data. Along with that I need to plot the data with an offset at different location and i also need the offset distance as a scale either on top or on bottom X axis. I have tried plotting it but I am unable to retrive the desired result. Can someone please help me. My code is:
Z = readtable('Atq100_2.xlsx') ;
data = table2array(Z) ;
plot(data(:,2)+10, data(:,1),'linewidth', 2);
hold on
plot(data(:,4)+250, data(:,3),'linewidth', 2);
hold on
plot(data(:,6)+500, data(:,5),'linewidth', 2);
hold on
plot(data(:,8)+1000, data(:,7),'linewidth', 2);
hold on
plot(data(:,10)+1500, data(:,9),'linewidth', 2);
hold off
legend('x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm', 'Location', 'northeastoutside');

 Accepted Answer

One option is to use the subplot function in a loop —
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/745434/Atq100_2.xlsx') ;
data = table2array(Z) ;
plot(data(:,2)+10, data(:,1),'linewidth', 2);
hold on
plot(data(:,4)+250, data(:,3),'linewidth', 2);
hold on
plot(data(:,6)+500, data(:,5),'linewidth', 2);
hold on
plot(data(:,8)+1000, data(:,7),'linewidth', 2);
hold on
plot(data(:,10)+1500, data(:,9),'linewidth', 2);
hold off
legend('x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm', 'Location', 'northeastoutside');
N = size(data,2);
Nsp = N/2;
ttlc = {'x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm'};
figure
for k = 1:Nsp
subplot(1,Nsp,k)
col = [2 1]+2*(k-1);
plot(data(:,col(1)), data(:,col(2)), 'LineWidth',2)
grid
title(ttlc{k})
ylim([-1 1]*200)
end
I am not certain what the desired result is.
.

8 Comments

Tnak you for helping it out. Is there any way to set an continuous axis for x? I mean from 0 to 1500 without breaking it? It would be somuch helpful if you can help me out in that way. And Instead of having two figures is it possible to have only one figure? Thank you in advance.
My pleasure!
Is there any way to set an continuous axis for x? I mean from 0 to 1500 without breaking it?
That is what you did in your original code.
I thought that you wanted a way to separate the plots horizontally in different axes.
I now have no idea what you want. Please describe what you want it a bit more detail.
.
I am attaching the figure what I want. So wat I want is i need to have an axis jus like you have created in subplots in this plot. Foe example in subplot figure, for the 1st plot there is 0-50 scale range on the x axis. I want the same as a single vector scale as i have shown in the attachement. I hope you understand what I need.
With regards
I still do not understand what you want.
Try this —
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/745434/Atq100_2.xlsx') ;
data = table2array(Z) ;
N = size(data,2);
Nsp = N/2;
ttlc = {'x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm'};
xofst = [10 250 500 1000 1500];
figure
ylim([-1 1]*200)
xlim([0 1600])
hold on
for k = 1:Nsp
col = [2 1]+2*(k-1);
plot(data(:,col(1))+xofst(k), data(:,col(2)), 'LineWidth',2)
minx = min(data(:,col(1))+xofst(k));
maxx = max(data(:,col(1))+xofst(k));
plot([1;1]*[minx maxx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
end
hold off
legend(ttlc, 'Location','northeastoutside')
% grid
% title(ttlc{k})
% ylim([-1 1]*200)
Experiment to get the result you want.
.
Thank you for your quick response and the help. what you have done is the desired result i need. But i need one extra thing which is, is it possible to increase the width of the each plot area which represents the exact data. For example the data at x = 1000 looks more like a widen curve as show in the attachment. But in the current plot it looks like a nearly straight line. So my idea is if we increase the width of the dotted line which has been drawn may give us the desired curved profil. I still dont know is it possible to do so or not.
My pleasure!
The plot can become uncomplicated and difficult to interpret. I introduced an amplification factor ‘famp’ (since I could not think of anything else to call it) that expands the ‘x’ range of each series. (I would not increase that beyond the current value of 2 for obvious reasons.) The new ‘catacol1’ variable incorporates it, and substitutes for the first argument to plot. The rest of the code uses it as well.
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/745434/Atq100_2.xlsx') ;
data = table2array(Z) ;
N = size(data,2);
Nsp = N/2;
ttlc = {'x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm'};
xofst = [10 250 500 1000 1500];
figure
ylim([-1 1]*200)
xlim([0 1600])
hold on
for k = 1:Nsp
col = [2 1]+2*(k-1);
famp = 5; % Amplification Factor
datacol1 = data(:,col(1))*famp+xofst(k);
hp(k) = plot(datacol1, data(:,col(2)), 'LineWidth',2);
% plot(data(:,col(1))+xofst(k), data(:,col(2)), 'LineWidth',2)
% minx = min(data(:,col(1))+xofst(k));
% maxx = max(data(:,col(1))+xofst(k));
minx = min(datacol1);
maxx = max(datacol1);
plot([1;1]*[minx maxx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
end
hold off
hl = legend([hp], ttlc, 'Location','northoutside', 'Orientation','horiz', 'NumColumns',2);
% lgdpos = hl.Position
% hl.Position = lgdpos+[0.134 0 0 0];
ht = title('Velocity Profile At Q = 100');
ttlpos = ht.Position;
ht.Position = ttlpos+[0 100 0];
xticks(xofst) % Optional
Repositioning the legend (and adjusting the title position) also improves the horizongal separation. I also added the sticks call to make the plot a bit easier to interpret. Use those changes if you want them.
.
Thank you so much. It looks more like the desired result i need. I am glad and thankful for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!