You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to combine two plots into one
5 views (last 30 days)
Show older comments
I am using MATLAB R2018 b. I have two plots with same x but, the limits of y axes are different. Both plots has a line and some data points (different color of the line and shape of the markers). These two plots are output from two different scripts. I want to combine these two plots to compare my results.
I am unable to use the dock figure option which was suggested for one such similar questions in the community.
Any help would be appreciated.
Accepted Answer
Star Strider
on 24 Aug 2019
Plot both data series on the same axes either in the same plot call, or use the hold function with two plot calls.
14 Comments
Star Strider
on 24 Aug 2019
In what form do you have the plots? Are they in .fig files?
Star Strider
on 24 Aug 2019
It is straightforward to recover the data from .fig files.
Here is some code I wrote for a similar problem:
I = openfig('YourFigure.fig');
Ax = gca;
lgnd = Ax.Legend;
Lines = findobj(Ax,'Type','line');
for k1 = 1:fix(numel(Lines)/6) % Consider 6 ‘Lines’ In Each Iteration
for k2 = 1:6
X(k2,:) = Lines(k2+(k1-1)*6).XData; % Get ‘X’ Values For This Set
Y(k2,:) = Lines(k2+(k1-1)*6).YData; % Get ‘Y’ Values For This Set
end
Xm(k1,:) = X(1,:); % X-Vector (For Plot)
Ym(k1,:) = mean(Y); % Y-Vector (For Plot) - Uses ‘mean’, ‘median’ Or Others May Be Appropriate As Well
end
figure
semilogy(Xm', Ym')
grid
legend(Ax.Legend.String)
xlabel(Ax.XLabel.String)
ylabel(Ax.YLabel.String)
Do that for each of your .fig files, save the ‘X’ and ‘Y’ arrays separately for each file (name them ‘X1‘, ‘Y1’ and ‘X2’, ‘Y2’ for each figure as appropriate, since you only have two figures), then analyse the ‘X’ and ‘Y’ data as you wish. You can then plot them together as well, as the figure call in my code illustrates.
Even though I do not have your .fig files, this code should be robust enough to work with them.
Star Strider
on 24 Aug 2019
It took a few minutes to adapt my previous code to your figures.
Try this:
fignames = {'Track_50_PDF_Ux.fig'; 'Track_100_PDF_Ux.fig'};
for k2 = 1:numel(fignames)
SSF = openfig(fignames{k2});
Ax = gca;
Lines = findobj(Ax,'Type','line');
for k1 = 1:numel(Lines)
X{k2,:}(k1,:) = Lines(k1).XData; % Get ‘X’ Values For This Set
Y{k2,:}(k1,:) = Lines(k1).YData; % Get ‘Y’ Values For This Set
end
figure
plot(X{k2}(1,:), Y{k2}(1,:),'p', X{k2}(2,:), Y{k2}(2,:),'-')
grid
xlabel(Ax.XLabel.String)
ylabel(Ax.YLabel.String)
end
The ‘X’ and ‘Y’ cell arrays contain the information you want. The data for the first plot are in ‘X{1}’ and ‘Y{1}’, and ‘X{2}’ and ‘Y{2}’ for the second plot. (You can similarly save the x-label strings and y-label strings if you want to. I demonstrate how to get them in the plot calls.) See the documentation on Access Data in Cell Array for details on how to work with cell arrays if you are not familiar with them.
SS
on 24 Aug 2019
Edited: SS
on 24 Aug 2019
Thanks for the reply. I am new to MATLAB. I tried it but, it didn't generate the combined figure. It is generating the original figure and another figure with different shape of the markers of the former. Is there something that, I have to add to the above code?
Star Strider
on 24 Aug 2019
Add this after my previous code:
figure
hold all
for k = 1:numel(X)
plot(X{k}(1,:), Y{k}(1,:),'p', X{k}(2,:), Y{k}(2,:),'-')
end
hold off
title('Combined Data')
xlabel(Ax.XLabel.String)
ylabel(Ax.YLabel.String)
The complete code is now:
fignames = {'Track_50_PDF_Ux.fig'; 'Track_100_PDF_Ux.fig'};
for k2 = 1:numel(fignames)
SSF = openfig(fignames{k2});
Ax = gca;
Lines = findobj(Ax,'Type','line');
for k1 = 1:numel(Lines)
X{k2,:}(k1,:) = Lines(k1).XData; % Get ‘X’ Values For This Set
Y{k2,:}(k1,:) = Lines(k1).YData; % Get ‘Y’ Values For This Set
end
figure
plot(X{k2}(1,:), Y{k2}(1,:),'p', X{k2}(2,:), Y{k2}(2,:),'-')
grid
xlabel(Ax.XLabel.String)
ylabel(Ax.YLabel.String)
end
figure
hold all
for k = 1:numel(X)
lgndstr{k,1} = sprintf('Figure %d Data',k);
lgndstr{k,2} = sprintf('Figure %d Line',k);
plot(X{k}(1,:), Y{k}(1,:),'p', X{k}(2,:), Y{k}(2,:),'-')
end
hold off
title('Combined Data')
xlabel(Ax.XLabel.String)
ylabel(Ax.YLabel.String)
legend(lgndstr{1,:}, lgndstr{2,:})
producing this plot:
That should do what you want.
Star Strider
on 25 Aug 2019
As always, my pleasure!
Star Strider
on 25 Aug 2019
Hi!
Add this line somewhere after the plot call loop:
set(gca, 'Box','on')
Star Strider
on 26 Aug 2019
As always, my pleasure.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)