2017-05-25 249 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