2016-07-06 17 views
0

Ich habe Schwierigkeiten, die Schriftgröße in der Legende eines Diagramms in Matlab R2016a zu ändern. Wenn ich die bevorzugte Syntax l = legende() verwende, funktioniert es korrekt. Ich benötige jedoch Zugriff auf die Symbole, um die Facea-Eigenschaft zu ändern. Daher verwende ich die Syntax [l, icons, plots, txt] = legend(), die laut Matlab "nicht empfohlen wird und eine Legende erstellt, die nicht alle Grafikfunktionen unterstützt." Wenn Sie diese Syntax verwenden, wird die Schriftgröße nicht korrekt aktualisiert. Gibt es trotzdem die richtige Schriftgröße und transparente Legendensymbole?Matlab legende font size wird nicht aktualisiert, wenn [l, icons, plots, txt] = legende() verwendet wird

%% Some data to plot 
x=linspace(1,10); 
y=linspace(1,20); 
[xx,yy]=meshgrid(x,y); 
zz1=2*xx+3*xx.*yy+yy.^2; 

%% Correct font, but icons not transparent 
figure(1) 
h=surf(x,y,zz1,'FaceColor','b','EdgeColor','none'); 
alpha(h,0.4) 
l=legend('plot1'); 
l.FontSize=24; 
l.FontName='Wide Latin'; 

%% Icons transparent, but incorrect font 
figure(2) 
h=surf(x,y,zz1,'FaceColor','b','EdgeColor','none'); 
alpha(h,0.4) 
[l,icons,plot,text]=legend('plot1'); 
l.FontSize=24; 
l.FontName='Wide Latin'; 
set(findobj(icons,'type','patch'),'facea',0.4) 
+0

Symbol enthält auch die Textobjekte, deren Schriftgröße Sie ändern müssen. Ich weiß nicht, warum es nicht korrekt mit der Schriftgröße des Legendenobjekts verknüpft ist, scheint wie ein potenzieller Fehler. – excaza

Antwort

0

Im Aufruf mit mehreren Ausgängen sieht es so aus, als ob die Schriftgröße an die zweite Ausgabe von Legend angehängt ist. Versuchen Sie folgendes Beispiel:

%Plot some data and a legend 
[X,Y,Z] = peaks; 
surf(X,Y,Z) 
[LEGH,OBJH,OUTH,OUTM] = legend('Peaks legend'); 

%Change the transparency of the legend patches 
OBJH(2).FaceAlpha = 0.4; %Your "findobj" call is probably more reliable. 

%Change the fontsize 
OBJH(1).FontSize = 6; %Your "findobj" call is probably more reliable. 

%If you change the "fontsize" of the main legend object, 
% that seems to change the size of the legend box, but not the contents. 
LEGH.FontSize = 16; 
%This could actually be useful, for example to fine tune the size 
% of the box. But it is pretty counterintuitive. 

Um zu Ihrem Code diretly anwendbar zu sein:

%% Altogether now 
figure(3) 
h=surf(x,y,zz1,'FaceColor','b','EdgeColor','none'); 
alpha(h,0.4) 
[l,icons,plot,text]=legend('plot1'); 
%Set the objects that you want 
set(findobj(icons,'type','Text'),'FontSize',24) 
set(findobj(icons,'type','Text'),'FontName','Wide Latin') 
set(findobj(icons,'type','patch'),'facea',0.4) 

%Make painful adjustments to item positioning (there should be a better way ...) 
l.FontName='Wide Latin'; %These two lines get the box about the right size 
l.FontSize=24; 
icons(2).Vertices([3 4],1) = 0.2; %This squeezes the color patch to the left 
icons(1).Position(1) = 0.3;  %This moves the text to the left to fit in the box 

(Ich laufe derzeit R2015a Dieses feine detaillierte Verhalten kann etwas ändern in Ihrer aktualisierten Version..)

0

Hatte das gleiche Problem - ich konnte Symbole (ii) .FontSize in einer Schleife ausführen, indem Sie durch oder von der Befehlszeile aus, aber würde nicht in einem Skript reagieren.
Ich habe eine Pause (0.1) vor meiner Schleife hinzugefügt, und voila!