手动绘制带有标记和字符串的图例

时间:2012-08-25 15:43:07

标签: matlab marker legend

我需要使用一组标记和相关字符串分别绘制图例,而不使用绘图。有没有人见过这个代码?

问候

3 个答案:

答案 0 :(得分:2)

我之前尝试过这样做,但我没有成功。我最终做的是从图例中删除一些情节元素。例如:

h = plot(x,y); 
hasbehavior(h,'legend', false);

这是另一个example

答案 1 :(得分:1)

这是我最终提出的代码:它几乎适用于任何应用程序:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%     Legend Generator       %%%%%%%%%%%%%%%%%%%%%
%% NAME     : LEGEND_GENERATOR.m
%% FUNCTION : Generates a single plot for arbitrary legend 
%% INPUTS   : set of markers and related texts as cell and figure number
%% OUTPUTS  : No output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%                      %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%     AUG 28, 2012      %%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function LEGEND_GENERATOR(Marker,text,Fignum,FONTSIZE,MARKERSIZE,LWD)

% clc;clear;close all;fclose all
% Marker = {'r -*' , 'b -o' , 'g --d'};
% text = {'First Fun ' , 'Second Fun ' , 'Third Fun ' };
% FONTSIZE = 6;
% Fignum = 26;

if length(Marker)~=length(text)
    error('You should have the same number of markers as texts');
end

%% FIGURE
set(0,'defaultaxesfontsize',FONTSIZE);
figure(Fignum);clf;hold on;set(gcf,'OuterPosition',[60  90 200  250],'PaperPositionMode','auto');


Leg_TEXTS = [];
for i = 1:length(Marker)
x = 0:0.1:2*pi;
y = 3*randn(1)*sin(x);
p = plot(x,y,Marker{i},'MarkerSize',MARKERSIZE,'LineWidth',LWD);
Leg_TEXTS = [ Leg_TEXTS ; {text{i}} ];
end


legend(Leg_TEXTS);
figure(Fignum);
set([gca;get(gca, 'children')], 'visible', 'off');

end

答案 2 :(得分:0)

您可以随时隐藏轴及其所有子项:

figure(1), clf, hold on

x = 0:0.1:2*pi;
y = sin(x);

p = plot(x,y);
legend('Sin(x)');

set([gca get(gca, 'children')], 'visible', 'off');