Reputation: 1
I have the following problem: when plotting 2 figures, I get the wrong legend for the second figure, in the sense that the linetypes ('-', '--', ':')
are not shown in the legend. The graph, however, shows 3 different lines correctly with styles ('-', '--', ':')
.
Strangely, the legend for the first figure works and shows the different linetypes, even though I am literally using the same code to format the chart.
Do you see the error, or is this a version bug (I am using R2016b)? Is there a way to "force" the legend entry styles as a workaround?
Please see my code below.
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')
Here is a minimum example, which surprisingly works fine:
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')
The following problem shows that the legend types are not equal to the lines shown in the graph:
EDIT2: if I comment 2 plots and leave only one line to be drawn, the style of the legend is the one of the line drawn. For example, if I only draw the second line, it looks like below:
Upvotes: 0
Views: 1562
Reputation: 211
The code below works for me, version 2016a, if you have any questions, just ask.
What I basically did is add handles to the lines you're plotting and making sure that the legend entries text is entered into a cell array instead of different arguments into the legend call (of matlab). You also have a bunch of unnecessary "hold on" calls but I didn't remove those.
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');
Upvotes: 2
Reputation: 30175
In your second edit, there is some strange behaviour. If you plot one line, then call for a legend with 3 labels, it should ignore the extra labels (and tell you it is doing so!)
This means the legend is only treating your labels as all belonging to the first (or only) line, whatever that style is.
To resolve this, try:
legend
call on the line immediately after the final plot
. \alpha
) in case the TeX rendering is an issue%
in case that is causing an issue (for some unknown reason!)Simplifying the name-value pair arguments by using things like
plot(x,CFaR3_shift, ':b', 'LineWidth',1.5)
As per the documentation, you should provide the multiple labels in a cell array if you are also using name-value arguments (like the line style)
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)
.
So in your case, call
legend ({'\alpha = 1%', '\alpha = 5%', '\alpha = 10%'}, 'Location','southoutside', 'Orientation','horizontal')
Also as an aside, you can just call hold on
once per figure.
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
Upvotes: 0