2017-03-29 10 views
0

フレキシブルな凡例を追加しようとしています(図面の長さを短くし、線とテキストの間の不要なスペースを抑えようとしています)、別の投稿の手順を使用しました:advanced plotting (legend manipulation) in Matlabコードはうまく動作しますが、凡例の変更後にフルスクリーンで図を表示しようとすると、私がしたことは無意味です。MATLABでフレキシブル凡例を追加

x = randn(6,20); 

figure(2) 
hax = gca; 

plot(x(1,:),'--k','linewidth',1.5); 
hold on; 
plot(x(2,:),'b','linewidth',1.5); 
% hold on; 
plot(x(3,:),'g','linewidth',1.5); 
% hold on; 
plot(x(4,:),'r','linewidth',1.5); 
% hold on; 
plot(x(5,:),'c','linewidth',1.5); 
% hold on; 
plot(x(6,:),':r','linewidth',1.5); 


ylabel('states','fontsize',14); xlabel('time(s)','fontsize',10); 
%legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14); 
% 
% New call "legend" 
% 
[leg_h,leg_item_h,~,~]=legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14); 
% 
% legendshrink(0.8,[]); 
%Fig_legend = legend('Taylor','Euler','LLscheme','LLscheme1'); 
%set(Fig_legend,'FontSize',7) 

grid(hax,'on') 
% axis(hax,'tight') 
set(hax,'box','on','Layer','top'); 
set(hax,'tickdir','out') 

%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% GENERATION OF THE LEGEND % 
%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Define a scale factor fot the lines 
line_scale_factor=1.4; 
% Define a scale factor fot the lines 
text_scale_factor=1.35; 
% Get the "Position" of the legend 
orig_leg_pos=get(leg_h,'position') 
% Get the number on objects in the legend 
n_obj=length(leg_item_h); 
% Extract the "Line" object 
line_obj=leg_item_h(n_obj/3+1:2:n_obj); 
% Get the "LineStyle" of each "Line" in the legend 
l_style=get(line_obj,'LineStyle') 
% Get the "Color" of each "Line" in the legend 
l_col=cell2mat(get(line_obj,'color')) 
% Get the "XData" and "YData" of the "Lines" in the legend 
leg_x_data=cell2mat(get(line_obj,'xdata')) 
leg_y_data=cell2mat(get(line_obj,'ydata')) 
% Get the handle of the "Text" of the items in the legend 
leg_t=leg_item_h(1:n_obj/3) 
% Get the "Text" of the items in the legend 
str=get(leg_t,'string') 
% Get the "Position" of each "Text" item in the legend 
tx=cell2mat(get(leg_t,'position')) 
% Delete the original legend 
delete(leg_h) 
% Create an axes with the same position and size of the original legend 
ax=axes('position',orig_leg_pos,'xlim',[0 1],'ylim',[0 1], ... 
    'xtick',[],'ytick',[],'box','on') 
hold on 
% Add the legend items to the axes 
for i=1:n_obj/3 
    % Add the lines with the original settings (color, style, ...) 
    plot([leg_x_data(i,1) leg_x_data(i,2)/line_scale_factor],leg_y_data(i,:),'color',l_col(i,:), ... 
          'linestyle',l_style{i}, ... 
          'linewidth',1.4) 
    % Add the text 
    th(i,:)=text(leg_x_data(i,2)/text_scale_factor,tx(i,2),0,str{i},'fontsize',9, ... 
      'unit','normalized') 
end 
% Get the maximun extent of the lagend "Text" 
tx_max_ext=max(reshape([th(:).Extent],4,6)'); 
% Evaluate the axis scaling factor 
tx_r_1=tx_max_ext(3)+leg_x_data(i,2)/line_scale_factor 
% Get the axes position 
axp=ax.Position 
% Resize the axes width 
ax.Position=[axp(1) axp(2) axp(3)*tx_r_1 axp(4)] 

答えて

0

あなたはfigureResizeFcnに「新たな伝説」を作成し、上記のコードのセクションを移動することによって、あなたの目標を達成することができます。

図のサイズが変更されるたびに、軸の凡例のサイズが適切に設定されます。そうするには

、あなたは、このようにコードを変更する必要があります。

1)数字にtagを割り当て、これは数字にアクセスするためにResizeFcnに使用されます。

cf=figure('tag','res_leg') 

2)figureオブジェクト

% Set in the FIGURE GUIDATA the legend handles 
my_guidata=guidata(cf) 
my_guidata.leg_h=leg_h; 
my_guidata.leg_item_h=leg_item_h 
% Store the GUIDATA data 
guidata(cf,my_guidata) 

3)お尻にオリジナルlegendハンドルを格納するguidataを使用ちょうどResizeFcnとして名前functionにあなたはLEGEND%ののセクション%世代のセクションに今持っているコードを移動ResizeFcn

の定義)ResizeFcnfigure

set(cf,'ResizeFcn',['doResizeFcn(''' get(gcf,'tag') ''')']) 

4にIGN (この場合はdoResizeFcn

上記のコードを次のように更新します(コードの順番で):

4.1)figure

4.2のハンドルを取得します)guidata

4.3を使用してfigureデータをRetrie)図は、すでに伝説の代わりに使用axesが含まれているか確認してください。もしそうなら、それ

4.4)は、元伝説

4を交換するfigureで作成axesのハンドルをguidata

4.5)ストアをguidataを使用して、元の凡例データをRetrie削除。上記のステップはプロット

x = randn(6,20); 
%%%%%%%%%% 
% STEP 1 % 
%%%%%%%%%% 
% Create the FIGURE and assign a TAG to it (to be used by FINDOBJ) 
cf=figure('tag','res_leg') 

hax = gca; 

plot(x(1,:),'--k','linewidth',1.5); 
hold on; 
plot(x(2,:),'b','linewidth',1.5); 
% hold on; 
plot(x(3,:),'g','linewidth',1.5); 
% hold on; 
plot(x(4,:),'r','linewidth',1.5); 
% hold on; 
plot(x(5,:),'c','linewidth',1.5); 
% hold on; 
plot(x(6,:),':r','linewidth',1.5); 


ylabel('states','fontsize',14); xlabel('time(s)','fontsize',10); 
%legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14); 
% 
% New call "legend" 
% 
[leg_h,leg_item_h,~,~]=legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14); 
% 
% legendshrink(0.8,[]); 
%Fig_legend = legend('Taylor','Euler','LLscheme','LLscheme1'); 
%set(Fig_legend,'FontSize',7) 

grid(hax,'on') 
% axis(hax,'tight') 
set(hax,'box','on','Layer','top'); 
set(hax,'tickdir','out') 

%%%%%%%%%% 
% STEP 2 % 
%%%%%%%%%% 
% Set in the FIGURE GUIDATA the legend handles 
my_guidata=guidata(cf) 
my_guidata.leg_h=leg_h; 
my_guidata.leg_item_h=leg_item_h 
% Store the GUIDATA data 
%%%%%%%%%% 
% STEP 3 % 
%%%%%%%%%% 
guidata(cf,my_guidata) 
%%%%%%%%%% 
% STEP 4 % 
%%%%%%%%%% 
% Assign a RESIZE function the the figure 
set(cf,'ResizeFcn',['doResizeFcn(''' get(gcf,'tag') ''')']) 

ため

%%%%%%%%%%%% 
% STEP 4.1 % 
%%%%%%%%%%%% 

オリジナルコードとコードにマークされている - 6)以下の全体更新されたコードでは、フィギュアのguidataを

を保存図のResizeFnc

function doResizeFcn(str) 
%%%%%%%%%%%% 
% STEP 4.1 % 
%%%%%%%%%%%% 
% Get the handle of the calling FIGURE 
curr_fig=findobj('tag',str); 
%%%%%%%%%%%% 
% STEP 4.2 % 
%%%%%%%%%%%% 
% Retrieve the GUIDATA 
my_guidata=guidata(curr_fig) 
%%%%%%%%%%%% 
% STEP 4.3 % 
%%%%%%%%%%%% 
% Check if the RESIZE function has been already called 
if(isfield(my_guidata,'ax')) 
    % if so, delete the previously created axes (used in place of the 
    % legend) 
    delete(my_guidata.ax) 
end 
%%%%%%%%%%%% 
% STEP 4.4 % 
%%%%%%%%%%%% 
% Get the old legend handles to retrieve the original legend data 
leg_h=my_guidata.leg_h; 
leg_item_h=my_guidata.leg_item_h 

line_scale_factor=1.4; 
% Define a scale factor fot the lines 
text_scale_factor=1.35; 
% Get the "Position" of the legend 
orig_leg_pos=get(leg_h,'position') 
% Get the number on objects in the legend 
n_obj=length(leg_item_h); 
% Extract the "Line" object 
line_obj=leg_item_h(n_obj/3+1:2:n_obj); 
% Get the "LineStyle" of each "Line" in the legend 
l_style=get(line_obj,'LineStyle') 
% Get the "Color" of each "Line" in the legend 
l_col=cell2mat(get(line_obj,'color')) 
% Get the "XData" and "YData" of the "Lines" in the legend 
leg_x_data=cell2mat(get(line_obj,'xdata')) 
leg_y_data=cell2mat(get(line_obj,'ydata')) 
% Get the handle of the "Text" of the items in the legend 
leg_t=leg_item_h(1:n_obj/3) 
% Get the "Text" of the items in the legend 
str=get(leg_t,'string') 
% Get the "Position" of each "Text" item in the legend 
tx=cell2mat(get(leg_t,'position')) 
% Delete the original legend 
% % % delete(leg_h) 
leg_h.Visible='off' 
% Create an axes with the same position and size of the original legend 
ax=axes('position',orig_leg_pos,'xlim',[0 1],'ylim',[0 1], ... 
    'xtick',[],'ytick',[],'box','on') 

%%%%%%%%%%%% 
% STEP 4.5 % 
%%%%%%%%%%%% 
% Store in the FIGURE GUIDATA the axes handle 
my_guidata.ax=ax; 
%%%%%%%%%%%% 
% STEP 4.6 % 
%%%%%%%%%%%% 
% Store the FIGURE GUIDATA 
guidata(curr_fig,my_guidata) 

hold on 
% Add the legend items to the axes 
for i=1:n_obj/3 
    % Add the lines with the original settings (color, style, ...) 
    plot([leg_x_data(i,1) leg_x_data(i,2)/line_scale_factor],leg_y_data(i,:),'color',l_col(i,:), ... 
          'linestyle',l_style{i}, ... 
          'linewidth',1.4) 
    % Add the text 
    th(i,:)=text(leg_x_data(i,2)/text_scale_factor,tx(i,2),0,str{i},'fontsize',9, ... 
      'unit','normalized') 
end 
% Get the maximun extent of the lagend "Text" 
tx_max_ext=max(reshape([th(:).Extent],4,6)'); 
% Evaluate the axis scaling factor 
tx_r_1=tx_max_ext(3)+leg_x_data(i,2)/line_scale_factor 
% Get the axes position 
axp=ax.Position 
% Resize the axes width 
ax.Position=[axp(1) axp(2) axp(3)*tx_r_1 axp(4)] 
end 

希望します。

Qapla '