Matlab多個Figure圖合成一個Fig
Matlab多個Figure圖合成一個Fig
轉自:http://jacoxu.com/matlab%E5%A4%9A%E4%B8%AA%E5%B7%B2figure%E5%9B%BE%E5%90%88%E6%88%90%E4%B8%80%E4%B8%AA/
案例:之前跑過的程式 已經生成了多個matlab圖,現在需要進行合併到一個圖中。
解決方案,利用影象控制代碼把figure影象中的引數讀入到記憶體中,然後重新subplot繪製。
clc;clear;
open(‘./test_1.fig’)
figure_info=findall(gcf,’type’,'line’);
xdata1 = get(figure_info,’xdata’);
ydata1 = get(figure_info,’ydata’);
color1 = get(figure_info,’color’);
subNum1 = length(xdata1);
open(‘./test_2.fig’)
figure_info=findall(gcf,’type’,' line’);
xdata2 = get(figure_info,’xdata’);
ydata2 = get(figure_info,’ydata’);
color2 = get(figure_info,’color’);
subNum2 = length(xdata2);
open(‘./test_3.fig’)
figure_info=findall(gcf,’type’,'line’);
xdata3 = get(figure_info,’xdata’);
ydata3 = get(figure_info,’ydata’);
color3 = get(figure_info,’color’);
subNum3 = length(xdata3);
open(‘./test_4.fig’)
figure_info=findall(gcf,’type’,' line’);
xdata4 = get(figure_info,’xdata’);
ydata4 = get(figure_info,’ydata’);
color4 = get(figure_info,’color’);
subNum4 = length(xdata4);
%%
subplot(2,2,1)
for i=subNum1:-1:1
if length(xdata1{i})==1
break;
end
plot(xdata1{i},ydata1{i},‘.’,’color’,color1{i} )
hold on;
end
title(‘(a). K-means (TF-IDF)’)
set(gca,’xtick’,[]);
set(gca,’ytick’,[]);
% box off;
% axis off;
subplot(2,2,2)
for i=subNum2:-1:1
if length(xdata2{i})==1
break;
end
plot(xdata2{i},ydata2{i},‘.’,’color’,color2{i})
hold on;
end
title(‘(b). Spectral Clustering (best)’)
set(gca,’xtick’,[]);
set(gca,’ytick’,[]);
% box off;
% axis off;
subplot(2,2,3)
for i=subNum3:-1:1
if length(xdata3{i})==1
break;
end
plot(xdata3{i},ydata3{i},‘.’,’color’,color3{i})
hold on;
end
title(‘(c). Average Embedding (TF)’)
set(gca,’xtick’,[]);
set(gca,’ytick’,[]);
% box off;
% axis off;
subplot(2,2,4)
for i=subNum4:-1:1
if length(xdata4{i})==1
break;
end
lineH(subNum4-i+1) = plot(xdata4{i},ydata4{i},‘.’,’color’,color4{i});
hold on;
end
title(‘(d). STCC’)
set(gca,’xtick’,[]);
set(gca,’ytick’,[]);
% box off;
% axis off;
hL=legend(lineH,{’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′});
newPosition = [0.4 0.4 0.2 0.2];
newUnits = ’normalized’;
set(hL,’Position’, newPosition,’Units’, newUnits);
合併成單幅影象之後要做的另外一件美觀的事情就是調整間距了。。。
1 程式碼實現
figure(‘Name’,’預設’);
subplot(2,2,1);
subplot(2,2,2);
subplot(2,2,3);
subplot(2,2,4);
figure(‘Name’,’緊湊’);
subplot(‘Position’,[0.02 0.65 0.3 0.3]);
subplot(‘Position’,[0.35 0.65 0.3 0.3]);
subplot(‘Position’,[0.02 0.3 0.3 0.3]);
subplot(‘Position’,[0.35 0.3 0.3 0.3]);
subplot(‘Position’,[left bottom width height]) creates an axes at the position specified by a four-element vector. left, bottom, width, and height are in normalized coordinates in the range from 0.0 to 1.
在由四個歸一化座標規定的位置上建立座標軸。
2 手動調整
在圖上的工具欄中點Tools,Align Distribute Tool,自己設定間距。
注意:1)需要先選中兩幅圖片,再調整間距;2)一定要用‘shift+click’選中待調整的兩幅圖片,不可以直接點
其實方式1 是一種非常讚的 版面設計方法,為了能夠更加清楚明瞭的理解 那四個引數,[left,bottom,width,height]是什麼意思,我畫了一張圖給大家,應該可以一目瞭然:
其中,第一幅圖的引數為[0.02, 0.65, 0.3, 0.3]