How to make lines plotted as dots for discontinuous data more visible in legend?
Show older comments
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
More Answers (1)
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)
1 Comment
Anthony Zara
on 17 Apr 2021
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!