2016-09-18 17 views
2

問題圖例框中不居中

我創建使用plotyy如圖片示出在其框不居中的情節的說明:

enter image description here

這是第一次這發生在我身上,而且出口到各種形式的問題後,問題仍然存在。我正在使用Matlab R2016a。

代碼

我用下面的代碼繪製的傳說:

a=sprintf('Test') 
b=sprintf('Test.\nTest Test') 
c=sprintf('Test\nTest') 
d=sprintf('TestTest\nTest') 
e=sprintf('Test\nTest') 
f=sprintf('Test\nTest Test') 
hLegend=legend([l1,l2,r1,r2,r3,r4], a, b, c, d, e, f); 
set([gca,hXLabel,hYLabel,hLegend] , 'FontName' , 'Helvetica','FontSize', 8) 
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal') 

因此這個問題,有沒有人有這個問題了嗎?有沒有辦法解決這個問題,或者我可以手動將圖例居中?

可執行的代碼示例

h=figure 
x=[1:10] 
y=[1:10] 
hold on 
yyaxis left 
l1=plot(x,y) 
l2=plot(x,2*y) 
hYLabel=ylabel('Test') 

yyaxis right 
r1=plot(x,y.^2) 
r2=plot(x,y.^3) 
r3=plot(x,10*y) 
r4=plot(x,20*y) 


hYLabel=ylabel('Test2') 
hXLabel = xlabel('TestTest]'); 
a=sprintf('Test') 
b=sprintf('Test.\nTest Test') 
c=sprintf('Test\nTest') 
d=sprintf('Test\nTest') 
e=sprintf('Test\nTest') 
f=sprintf('TestTest\nTest') 
hLegend=legend([l1,l2,r1,r2,r3,r4], a, b, c, d, e, f); 
set([gca,hXLabel,hYLabel,hLegend] , 'FontName' , 'Helvetica','FontSize', 8) 
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal') 
set(gca,'LineWidth',1.0) 
set(l1, 'LineWidth',1.5,'LineStyle','-') 
set(l2, 'LineWidth',1.5,'LineStyle','-.') 
set(r1, 'LineWidth',1.5,'LineStyle','-') 
set(r2, 'LineWidth',1.5,'LineStyle','-.') 
set(r3, 'LineWidth',1.5) 
set(r4, 'LineWidth',1.5,'LineStyle','- -') 

hold off 

雖然沒有顯示使用的原始數據,我用上面的例子中得到了同樣的問題。 非常感謝!

+0

我得到一個錯誤信息:*未定義的函數或變量「L1」 *你可以請張貼一個可執行的代碼示例? – Rotem

+0

@Rotem我加了一個可執行版本的代碼 – KiW

回答

1

我猜的問題是a=sprintf('Test')是單行,其他像b=sprintf('Test.\nTest Test')是多行。

當所有圖例條目都是兩條文本行時,文本居中正確。

解決方法:用a=sprintf('Test\n\0.')替換a=sprintf('Test')
這使得a兩條文本行,當第二行是一個空白字符。

enter image description here

+0

感謝您的回答,這已經是一個很大的幫助。你知道是否有辦法讓第一個集中在線路前?因爲在這種情況下,沒有第一個「測試」看起來有點丟失 – KiW

+0

不,我找不到設置拳頭標籤垂直對齊方式。 – Rotem

+0

替換順序 - (放置'Test'秒)解決了這個問題:'a = sprintf('Test'); hLegend = legend([l2,l1,r1,r2,r3,r4],b,a,c ,d,e,f);'(如果可以的話)。 – Rotem