2017-05-25 246 views
0

我有以下問題:繪製2個圖形時,我得到了第二個圖形的錯誤圖例,因爲線型('-', '--', ':')未顯示在圖例中。但是,該圖形與('-', '--', ':')款式正確顯示了3條不同的線條。Matlab:不正確的圖例

奇怪的是,第一個圖形的圖例工作和顯示不同的線型,即使我字面上使用相同的代碼來格式化圖表。

您是否看到錯誤,或者這是版本錯誤(我正在使用R2016b)?有沒有辦法「強制」圖例輸入樣式作爲解決方法?

請參閱下面的代碼。

figure plot(x,CR1, 'LineStyle', '-', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(x,CR2,'LineStyle', '--', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(x,CR3,'LineStyle', ':', 'Color', 'b','LineWidth',1.5) 
set(gca, 'xtick', [0:10:50]) 
xlabel('Fund lifetime (in quarters)') 
legend ('\alpha = 1%', '\alpha = 5%', '\alpha = 10%', 'location','southoutside', 'Orientation','horizontal') 

hold on 

% Figure 2 - here the problem where the legend is incorrect 
figure  
plot(x,CFaR1_shift, 'LineStyle', '-', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(x,CFaR2_shift,'LineStyle', '--', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(x,CFaR3_shift,'LineStyle', ':', 'Color', 'b','LineWidth',1.5) 
set(gca, 'xtick', [0:10:50]) 
xlabel('Fund lifetime (in quarters)') 
legend ('\alpha = 1%', '\alpha = 5%', '\alpha = 10%', 'Location','southoutside', 'Orientation','horizontal') 

這裏是一個最小例子,其令人驚訝地正常工作:

x=1:0.5:50 
y=x.^2; 

CR1=3*y; 
CR2=15*y; 
CR3=20*y; 

CFaR1_shift=10*y; 
CFaR2_shift=15*y; 
CFaR3_shift=20*y; 

figure 
plot(CR1, 'LineStyle', '-', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(CR2,'LineStyle', '--', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(CR3,'LineStyle', ':', 'Color', 'b','LineWidth',1.5) 
set(gca, 'xtick', [0:10:50]) 
xlabel('Fund lifetime (in quarters)') 
legend ('\alpha = 1%', '\alpha = 5%', '\alpha = 10%', 'location','southoutside', 'Orientation','horizontal') 

hold on 

% Figure 2 
figure  
plot(CFaR1_shift, 'LineStyle', '-', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(CFaR2_shift,'LineStyle', '--', 'Color', 'b','LineWidth',1.5) 
hold on 
plot(CFaR3_shift,'LineStyle', ':', 'Color', 'b','LineWidth',1.5) 
set(gca, 'xtick', [0:10:50]) 
xlabel('Fund lifetime (in quarters)') 
legend ('\alpha = 1%', '\alpha = 5%', '\alpha = 10%', 'Location','southoutside', 'Orientation','horizontal') 

以下問題表明圖例類型不等於在該圖中示出的線:

Screenshot

編輯2:如果我評論2個繪圖,並且只留下一行繪製,則圖例的樣式是繪製的線條之一。例如,如果我只畫了第二行,它看起來像如下:

EDIT2

+2

請嘗試構建一個[mcve]來演示您的問題。並非所有的變量都在您的代碼中定義。提供你的問題的圖像也可能有幫助。 – m7913d

+1

無法用R2016a重現它 –

+0

謝謝。我編輯了我的消息,包括一個最小的例子。令人驚訝的是,這裏的傳說顯示正確。你看到有什麼不同嗎? – mcpe

回答

0

在你的第二個編輯,有一些奇怪的行爲。如果您繪製一條線,然後要求用3個標籤的一個傳奇,它應該忽略額外的標籤(並告訴你它正在這樣做!)

legend

這意味着傳說是唯一的治療您的標籤作爲所有屬於第一(或唯一)行,不管那種風格。

要解決這個問題,嘗試:

  • 將上線legend呼叫的最終plot之後。只使用純文本
  • (除去\alpha)的情況下,TeX的渲染是一個問題
  • 去掉百分號%項導致的問題(一些未知的原因的情況下!)
  • 通過使用事簡化名稱 - 值對的參數等

    plot(x,CFaR3_shift, ':b', 'LineWidth',1.5) 
    

作爲每documentation,你應該如果您還使用名稱在單元陣列提供的多個標籤 - 值參數(如線型)

legend(labels,Name,Value) sets legend properties using one or more name-value pair arguments. You must specify the labels using a cell array; for example, legend({'A','B'},'FontSize',12) .

所以你的情況,請致電

legend ({'\alpha = 1%', '\alpha = 5%', '\alpha = 10%'}, 'Location','southoutside', 'Orientation','horizontal') 

而且順便說一句,你可以叫hold on一次每身影。

figure  
hold on 
plot(CFaR1_shift, 'LineStyle', '-', 'Color', 'b','LineWidth',1.5) 
plot(CFaR2_shift,'LineStyle', '--', 'Color', 'b','LineWidth',1.5) 
plot(CFaR3_shift,'LineStyle', ':', 'Color', 'b','LineWidth',1.5) 
hold off 
+0

謝謝你的評論。不幸的是,這並沒有解決問題...也重新啓動,沒有成功。 – mcpe

+0

請提供您的數據是什麼樣子的截圖,以便我們可以更好地瞭解問題。由於*你*不能再現問題,*我們*絕對不能,並且不清楚發生了什麼問題!你最小的例子似乎是相同的原始代碼,除了不知道輸入是什麼... – Wolfie

+0

親愛的沃爾菲,謝謝。我在我的初始文章中包含了一個截圖。我試圖提供一個只使用不同數據點的簡單例子,但其餘的類似。希望截圖幫助... – mcpe