Legend not matching graph in specific code

I used previous questions answers to get a template code working however, when I apply this to my real code, it doesn't change and outputs no errors so I am at a loss.
Here is my testing code which works correctly:
x=[0:1:20]
clf
hold on
L1=plot(x,2*x,'r')
L2=plot(x,3*x,'k')
L3=plot(x,4*x)
L4=plot(x,5*x,'k')
L5=plot(x,6*x,'b')
L6=plot(x,7*x,'k')
L7=plot(x,8*x)
L8=plot(x,9*x,'k')
L9=plot(x,10*x,'k')
L10=plot(x,11*x,'k')
L11=plot(x,12*x,'k')
legend([L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11],'2D velocities','6D velocities','10D velocities','Lower Nozzle','Uppper Nozzle','Centre Line','Upper Divergence Line','Lower Divergence Line','Upper Core','Lower Core','FontSize',6,'Location','best')
Here is my real code which doesn't work:
(I cannot provide the real data for it)
hold on
L1=plot(x_core,y_core1,'r');
L2=plot(x_core,y_core2,'r');
L3=plot(5*v_2+2*D,r_2);
L4=plot(5*v_6+6*D,r_6);
L5=plot(5*v_10+10*D,r_10);
L6=plot(x_nozzle,x_nozzle/4-15,'k');
L7=plot(x_nozzle,-x_nozzle/4+15,'k');
L8=plot(cl,cl*0);
L9=plot(div,div*topgrad+topint,'b');
L10=plot(div,div* botgrad+botint,'b');
L11=plot([0 0], ylim, 'k')
hold off
%applies axis labels
legend([L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11],'2D velocities','6D velocities','10D velocities','Lower Nozzle','Uppper Nozzle','Centre Line','Upper Divergence Line','Lower Divergence Line','Upper Core','Lower Core','Origin','FontSize',6,'Location','best')
I can't spot where the difference is.

5 comentarios

Walter Roberson
Walter Roberson el 14 de Abr. de 2020
In your test, you pass in 10 legends for 11 lines, and get a warning about extra entries being ignored.
In your real code, we do not have values to test with. We would have to know the size of the variables . Also "doesn't work" is not very specific...
Joshua Martin
Joshua Martin el 14 de Abr. de 2020
Apologies for not making it clear, the issue is that the legend does not match the colours assigned to the plots in the code in the 'real code'.
The data provided should be irrelevant in this case as the issue is in formatting and hence the testing code is given to provide an idea of what I am trying to achieve. I am aware of the 10 legends, 11 lines issue but this is intentional due to the nature of this graph.
Therefore, I am asking why in the testing code, the legend assigns the correct colours (I.e. the colours I have specifically assigned each line to group them) and the real code does not.
Joshua Martin
Joshua Martin el 14 de Abr. de 2020
Note: I have now removed L11 from being passed to the legend
Walter Roberson
Walter Roberson el 14 de Abr. de 2020
I would have thought that L8=plot(cl,cl*0); would be the Centre line, not L6=plot(x_nozzle,x_nozzle/4-15,'k'); ?
Joshua Martin
Joshua Martin el 14 de Abr. de 2020
Thank you, you are indeed correct the code works correctly now with some further reordering.
To clarify, my issue was that I was not seeing the correct pairs of colours, it just showed a rainbow from my perspective. By reordering them correctly, I realised that it did indeed show the colours just whilst zoomed out, the pair of reds looks like red and orange, and likewise for the blue and black sets. Hence the confusion.
Thank you again.

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Preguntada:

el 14 de Abr. de 2020

Comentada:

el 14 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by