이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to combine two plots into one
조회 수: 3 (최근 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?
Star Strider
2019년 8월 25일
Hi!
Add this line somewhere after the plot call loop:
set(gca, 'Box','on')
추가 답변 (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 (한국어)