Matlab:不正确的传奇

时间:2017-05-25 10:41:56

标签: matlab legend

我有以下问题:当绘制2个数字时,我得到第二个数字的错误图例,因为图例中没有显示线型('-', '--', ':')。但是,该图表使用样式('-', '--', ':')正确显示了3条不同的行。

奇怪的是,第一个数字的图例有效,并显示了不同的线型,即使我实际上使用相同的代码来格式化图表。

您是否看到错误,或者这是版本错误(我使用的是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

EDIT2:如果我评论2个绘图并且只留下一行要绘制,则图例的样式就是绘制的线条之一。例如,如果我只画第二行,它看起来如下:

EDIT2

2 个答案:

答案 0 :(得分:0)

在第二次编辑中,有一些奇怪的行为。如果您绘制一条线,然后调用带有3个标签的图例,它应该忽略额外的标签(并告诉您它正在这样做!)

legend

这意味着图例仅将您的标签视为属于第一行(或唯一)的行,无论该样式是什么。

要解决此问题,请尝试:

  • 在最终legend之后立即将plot电话放在线路上。
  • 仅使用纯文本(删除\alpha)以防TeX渲染出现问题
  • 在导致问题的情况下删除百分号%(原因不明!)
  • 使用

    之类的内容简化名称 - 值对参数
    plot(x,CFaR3_shift, ':b', 'LineWidth',1.5)
    

根据documentation,如果您还使用名称 - 值参数(如线条样式),则应在单元格数组中提供多个标签

  

legend(labels,Name,Value)使用一个或多个名称 - 值对参数设置图例属性。 您必须使用单元格数组指定标签;例如,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

答案 1 :(得分:0)

以下代码适用于我,版本2016a,如果您有任何疑问,请询问。

Graphs showing proper legends

我基本上做的是为你正在绘制的线添加句柄,并确保将图例条目文本输入到单元格数组中,而不是在图例调用(matlab)中输入不同的参数。 你还有一堆不必要的“保持”电话,但我没有删除它们。

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;

text={'\alpha = 1%', '\alpha = 5%', '\alpha = 10%'}; %text is the same for both legends so make 1 variable, has to be entered in a cell array

figure
h1=plot(CR1, 'LineStyle', '-', 'Color', 'b','LineWidth',1.5);
hold on
h2=plot(CR2,'LineStyle', '--', 'Color', 'b','LineWidth',1.5);
hold on
h3=plot(CR3,'LineStyle', ':', 'Color', 'b','LineWidth',1.5);
set(gca, 'xtick', [0:10:50])
xlabel('Fund lifetime (in quarters)')
hands=[h1,h2,h3]; %collect handles sometimes easier, can also be directly entered into legend
hlgd1=legend (hands,text, 'location','southoutside', 'Orientation','horizontal'); %handle so legend can be easily adjusted later on

hold on

% Figure 2
figure       
h4=plot(CFaR1_shift, 'LineStyle', '-', 'Color', 'b','LineWidth',1.5);
hold on
h5=plot(CFaR2_shift,'LineStyle', '--', 'Color', 'b','LineWidth',1.5);
hold on
h6=plot(CFaR3_shift,'LineStyle', ':', 'Color', 'b','LineWidth',1.5);
set(gca, 'xtick', [0:10:50])
xlabel('Fund lifetime (in quarters)')
hands2=[h4,h5,h6]; %collect handles sometimes easier, can also be directly entered into legend
hlgd2=legend (hands2,text,'Location','southoutside', 'Orientation','horizontal');