Legend for dashed line

I have plotted 6 different lines in my figure, 3 solid lines, and 3 dashed lines. When creating a legend however, all lines appear solid. Is there a fix to show the dashed lines (simulated values) as dashed in my legend?
figure(1)
plot(t_measurement,Mx*1000,'-r')
hold on
plot(t_measurement,My*1000, '-b')
plot(t_measurement,-Mz*1000, '-g')
plot(output_time(1:92),output_TB_Mx_BodyFrame(1:92),'--r')
plot(output_time(1:92),output_TB_My_BodyFrame(1:92),'--b')
plot(output_time(1:92),output_TB_Mz_BodyFrame(1:92), '--g')
legend('Measured Mx', 'Measured My', 'Measured Mz', 'Simulated Mx', 'Simulated My', 'Simulated Mz')
yaxis('Bending moment (Nm)')
xaxis('Time (s)')

6 Comments

i cannot reproduce the problem, it works for me. what happens if you call just legend() (without further arguments) and/or if you use a e.g. a dotted style instead of dashed line (maybe it is just a legend display error in thebway that we cant see the first space of the dashed line in the legend)
Rose
Rose on 28 Jul 2022
you are right, the dotted style worked. I have no clue why... I just leave it dotted.
Try
opengl('info')
and if 'Software' is false try
opengl('software')
and then retry the legend command.
To check out @Jonas's idea of resolution, try making the legend area much wider and see if then the line segments appear.
Which release MATLAB and OS are you using just for info purposes?
Is it possible that Mx, My, or Mz are 2D matrices? If they are then each plot() call for them would generate multiple lines.
dpb
dpb on 29 Jul 2022
@Walter Roberson -- good thought, but if were so, either
  1. The columns are identical-enough in values all points overlap, or
  2. The additional columns are NaN so don't plot, or
  3. YLIM() has been set so those lines aren't in the visible axis range, and
  4. There must be a total of at least six such columns collectively.
Otherwise, there would be more than the six visible lines in the plot. But, if one of those conditions were true, and the total number of columns >=6, then the symptoms would match.
@Rose, please fill out the "Products" (MATLAB) and "Release" fields on the right of this page.
Also, attach a mat file with the data so we can run this.

Sign in to comment.

Answers (1)

I suspect the issue is that you are missing a 'hold off' and have run your code multiple times. At least I could duplicate by running the first 3 plot commands, and then by running all the plot commands without closing the figure window. It occurred because hold was still 'on'. (I created this plot using dummy data)
It is best practice to always pair a 'hold on' with a corresponding 'hold off'.
% Create dummy data
t_measurement = linspace(0,0.9,10);
output_time = linspace(0,0.9);
output_TB_Mx_BodyFrame = 9e3+700*sin(output_time*40*pi);
output_TB_My_BodyFrame = 4.5e3+500*sin(output_time*40*pi);
output_TB_Mz_BodyFrame = -8e3+500*sin(output_time*40*pi);
Mx = 6+rand(size(t_measurement));
My = 4.5*ones(size(t_measurement));
Mz = 13-3*rand(size(t_measurement));
% Your plotting code (I added a 'hold off', anc change xaxis and yaxis to xlabel and ylabel
figure(1)
plot(t_measurement,Mx*1000,'-r')
hold on
plot(t_measurement,My*1000, '-b')
plot(t_measurement,-Mz*1000, '-g')
plot(output_time(1:92),output_TB_Mx_BodyFrame(1:92),'--r')
plot(output_time(1:92),output_TB_My_BodyFrame(1:92),'--b')
plot(output_time(1:92),output_TB_Mz_BodyFrame(1:92), '--g')
hold off
legend('Measured Mx', 'Measured My', 'Measured Mz', 'Simulated Mx', 'Simulated My', 'Simulated Mz','Location','east')
ylabel('Bending moment (Nm)')
xlabel('Time (s)')

Asked:

on 28 Jul 2022

Answered:

on 1 Aug 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!