How to make lines plotted as dots for discontinuous data more visible in legend?

36 views (last 30 days)
I have run into this issue several times when plotting discnotinuous data, and I though it was finally time to ask the question, as I have not been able to find a solution that works well besides writing an extra script to split my plots at discontinuities and plot normal lines. In order to avoid vertical lines where discontinuities occur, I have been plotting the data as points ('.') as my timesteps are sufficiently small for the plot to still appear as a line. This gets rid of the vertical line at discontinuity issue, but presents a new issue where the legend "line" (in this case dot) markers are very small, as seen in my figure.
This presents problems as they are difficult to interpret when inserted into my papers. (Yes, I understand that the angular velocity is continuous, I just plotted it in the same style to make the plots similar)
I am wondering if anyone knows of a way to override the legend in such a way that I can replace the tiny dots with a solid bar of the same color, like what would be displayed for an area plot.
Here is my code for the plot if anyone would like to modify it. I don't have the time to post the simulation which generated the plots, as it is a custom RK4 which calls a few other functions and that would just be too much.
set(groot, 'DefaultTextInterpreter', 'Latex')
plots.fig1 = figure('Renderer', 'painters', 'Position', [10 10 800 500]);
sgtitle(sprintf('Torque-Free Spacecraft Attitude Simulation for %g sec',t(end)),'FontSize',18)
subplot(2,1,1)
plot(t,Bw_BN.t(1,:),'.','DisplayName','${}^{\mathcal{B}}\omega_1$')
hold on, grid on
plot(t,Bw_BN.t(2,:),'.','DisplayName','${}^{\mathcal{B}}\omega_2$');
plot(t,Bw_BN.t(3,:),'.','DisplayName','${}^{\mathcal{B}}\omega_3$');
ylabel('${}^{\mathcal{B}}\mathbf{\omega}_{B/N}$ (rad/sec)','FontSize',14)
legend show
legend('Location','SouthEast','FontSize',12,'Interpreter','Latex')
subplot(2,1,2)
plot(t,sigma.t(1,:),'.','DisplayName','$\sigma_1$')
hold on, grid on
plot(t,sigma.t(2,:),'.','DisplayName','$\sigma_2$');
plot(t,sigma.t(3,:),'.','DisplayName','$\sigma_3$');
ylabel('$\mathbf{\sigma}_{B/N}$','FontSize',16)
legend show
legend('Location','SouthEast','FontSize',12,'Interpreter','Latex')
print(plots.fig1,'Attitude Simulation torquefree','-dpng')

Accepted Answer

Robert Brown
Robert Brown on 17 Apr 2021
Edited: Robert Brown on 17 Apr 2021
Following Chris LaPierre's advice above...
plot the graphs as a group with lines at only the first point, then plot the the graphs with dots on all points as a group. use the following command between the plots of the two groups to stop updating legend after plotting lines on the first dots !!!
legend('AutoUpdate','off')
I turned off the command in my code .... legend('AutoUpdate','off') which is discussed above...
if you want to turn it back on, uncomment it, and you will give you the desired results
------------------
A more detailed method of controlling legend is now explained here, implemented in code
If you follow my code, I will show you how to get a handle onto your legend and manipulate its variables through the handle !!!
Hope this helps !!! :-)
----------------------
clc; % clear command window
t = 1:100;
Bw_BN.t(1,:) = t.^1;
Bw_BN.t(2,:) = t.^1.5;
Bw_BN.t(3,:) = t.^2;
set(groot, 'DefaultTextInterpreter', 'Latex')
plots.fig1 = figure('Renderer', 'painters', 'Position', [1000 450 800 500]);
subplot(2,1,1)
hold on, grid on
ylabel('${}^{\mathcal{B}}\mathbf{\omega}_{B/N}$ (rad/sec)','FontSize',14)
plot(1,Bw_BN.t(1,1),'r-','DisplayName','1')
plot(1,Bw_BN.t(2,1),'g-','DisplayName','2');
plot(1,Bw_BN.t(3,1),'b-','DisplayName','3');
simple answer, uncomment this next line. Then legend will quit autoupdating and appending the new lines to the list of lines to be displayed in the legend !
% legend('AutoUpdate','off')
plot(t,Bw_BN.t(1,:),'r.','DisplayName','4')
plot(t,Bw_BN.t(2,:),'g.','DisplayName','5');
plot(t,Bw_BN.t(3,:),'b.','DisplayName','6');
A more detailed method of controlling legend is now explained here, implemented in code
If you follow my code, I will show you how to get a handle onto your legend and manipulate its variables through the handle !!!
Hope this helps !!! :-)
% turn on legend
legend
display(' ')
'note legend is on, and has 6 entries for the 6 plots, 3 plots with lines, 3 plots with dots'
'pausing now, press any key...'
pause
% get handle to legend, and show legend properties, note the 6 plot labels
% in "string"
legend_handle = legend
display(' ')
'get handle to legend, and show legend properties, note the 6 plot labels in "string" '
'pausing now, press any key...'
pause
% make legend forget labels 4-6
legend_handle.String = legend_handle.String(1:3)
display(' ')
'define legend_handle.String to have only the first 3 entries'
'note instantly on figure that legend only has 3 entries, 4-6 are gone'
'pausing now, press any key...'
pause
% make legend have fontsize 12
legend_handle.FontSize = 12
display(' ')
'define legend_handle.FontSize to be 12'
'note instantly on figure that legend font size becomes 12'
'pausing now, press any key...'
pause
% make legend 'Location' be 'SouthEast'
legend_handle.Location = 'SouthEast'
display(' ')
'define legend_handle.Location to be SouthEast'
'note instantly on figure that legend moves to SouthEast corner of plot'
'pausing now, press any key...'
pause
display(' ')
'here is legend_handle.Interpreter'
legend_handle.Interpreter
display(' ')
'Legend can be further manipulated in this manner,'
'as can other MATLAB objects that you assign a handle !!!'

More Answers (1)

Cris LaPierre
Cris LaPierre on 17 Apr 2021
Edited: Cris LaPierre on 17 Apr 2021
As I'm sure you are aware, a legend matches the formatspec of the corresponding line object. Since you plot without a line, your legend only shows a marker.
Consider plotting a single point of each series with a line style. The corresponding legend label will have a line. Then just be sure to specify which line objects to include in the legend.
a=rand(1,5);
l1 = plot(a(1),'-r','DisplayName','Line1');
hold on
plot(a,'.r','DisplayName','noName')
hold off
legend(l1)

Community Treasure Hunt

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

Start Hunting!