How to add a legend for a plot generated with a function multiple times?
    7 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Niccolò Bruno
 el 14 de Abr. de 2023
  
    
    
    
    
    Comentada: Niccolò Bruno
 el 14 de Abr. de 2023
            I am working with orbits around Earth. To plot them, I use a function with the orbital parameters as input. My problem is that I don't know how to create a legend when calling the function more than one time to show on the same plot multiple orbits.
Adding a legend manually like this 
plotOrbit(a, e, i, OM, om, th0, thf, dt, mu)
plotOrbit(a1, e1, i1, OM1, om1, th0, thf, dt, mu)
legend("Initial orbit", "Final orbit")
give me a wrong line for the second orbit. (It should be orange instead of a random blue tiny line).

I tried to follow a different approach
txt = 'Initial orbit';
plotOrbit(a, e, i, OM, om, th0, thf, dt, mu, txt)
txt = 'Final orbit';
plotOrbit(a1, e1, i1, OM1, om1, th0, thf, dt, mu, txt)
legend show
Adding this line in the function script
plot3(r(1, :), r(2, :), r(3, :), 'DisplayName',txt, LineWidth=2)
Now the legend shows the right lines, but the result shows 4 extra labels that I don't know how to remove.
 ù
ùDo you know how to solve this? Thank you
2 comentarios
  Steven Lord
    
      
 el 14 de Abr. de 2023
				It's going to be difficult to offer any specific suggestions without seeing what your plotOrbit function is doing.
Respuesta aceptada
  Antoni Garcia-Herreros
      
 el 14 de Abr. de 2023
        
      Editada: Antoni Garcia-Herreros
      
 el 14 de Abr. de 2023
  
      Hello,
As @Steven Lord pointed out it's hard to suggest anything without knowing how plotOrbit works, but you could try something like this:
plotOrbit(a, e, i, OM, om, th0, thf, dt, mu)
plotOrbit(a1, e1, i1, OM1, om1, th0, thf, dt, mu)
l=legend('Initial orbit','','','Final orbit');
Más respuestas (0)
Ver también
Categorías
				Más información sobre Legend en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


