How to combine two plots into one
4 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Star Strider
2019-8-24
Plot both data series on the same axes either in the same plot call, or use the hold function with two plot calls.
14 个评论
Star Strider
2019-8-24
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
2019-8-24
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.
Star Strider
2019-8-24
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.
SS
2019-8-25
Hi again. How can I have the axes lines on the right and top of the plot? To make it look like a box?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)