Could anyone help me how to have the legend as in the desired manner for two y axis.

2 visualizaciones (últimos 30 días)
Could anyone help me how to write the legend with two y axis as in the desired format.
I have generated the line graph with two y axis , 5 lines for lefy y axis and 5 lines for right y axis so total 10 lines
Now I want to have the legend in a manner that left y axis should be dotted line with different marker and right y axis should be solid line with the marker similar to left y axis.
Could anyone please help me on this.
  2 comentarios
KSSV
KSSV el 8 de Ag. de 2021
Show us your code. And first acknowledge the previous questions you have asked.
jaah navi
jaah navi el 8 de Ag. de 2021
x=1:10
y1=rand(1,10)
y2=rand(1,10)
y3=rand(1,10)
y4=rand(1,10)
figure(1)
[hAX,hLine1,hLine2] = plotyy(x, [y1; y3], x, [y2; y4], @(X,Y)semilogy(X,Y), @(X,Y)plot(X,Y));
set(hLine1(1),'LineStyle','-','Marker','*', 'Color', 'r');
set(hLine1(2),'LineStyle','-','Marker','o', 'Color', 'b');
set(hLine2(1),'LineStyle','--','Marker','*', 'Color', 'r');
set(hLine2(2),'LineStyle','--','Marker','o', 'Color', 'b');
set(gca,'XTick',[1:1:10])
xlabel('persons')
ylabel(hAX(1),'weight') % left y-axis
ylabel(hAX(2),'height') % right y-axis
grid on;
lgd=columnlegend(2,{'Batch 1','Batch 2 ','Batch 1','Batch 2'})
when I run the code i am getting the legend in the graph as follows
But actually i want 'Batch 1','Batch 2 ' to be displayed once with markers alone followed by solid line mentioning weights and dotted line mentioning heights.
Could you please help me on this.

Iniciar sesión para comentar.

Respuesta aceptada

Chunru
Chunru el 8 de Ag. de 2021
Change the last line
x=1:10;
y1=rand(1,10);
y2=rand(1,10);
y3=rand(1,10);
y4=rand(1,10);
figure(1)
[hAX,hLine1,hLine2] = plotyy(x, [y1; y3], x, [y2; y4], @(X,Y)semilogy(X,Y), @(X,Y)plot(X,Y));
set(hLine1(1),'LineStyle','-','Marker','*', 'Color', 'r');
set(hLine1(2),'LineStyle','-','Marker','o', 'Color', 'b');
set(hLine2(1),'LineStyle','--','Marker','*', 'Color', 'r');
set(hLine2(2),'LineStyle','--','Marker','o', 'Color', 'b');
set(gca,'XTick',[1:1:10])
xlabel('persons')
ylabel(hAX(1),'weight') % left y-axis
ylabel(hAX(2),'height') % right y-axis
grid on;
lgd=legend({'Batch 1','Batch 2 '});
  4 comentarios
jaah navi
jaah navi el 9 de Ag. de 2021
I want to have the legend as seen below. Could you please help me on this.
Chunru
Chunru el 9 de Ag. de 2021
Though I don't recommend such manual adjustment, it can be done:
x=1:10;
y1=rand(1,10);
y2=rand(1,10);
y3=rand(1,10);
y4=rand(1,10);
figure(1)
[hAX,hLine1,hLine2] = plotyy(x, [y1; y3], x, [y2; y4], @(X,Y)semilogy(X,Y), @(X,Y)plot(X,Y));
set(hLine1(1),'LineStyle','-','Marker','*', 'Color', 'r');
set(hLine1(2),'LineStyle','-','Marker','o', 'Color', 'b');
set(hLine2(1),'LineStyle','--','Marker','*', 'Color', 'r');
set(hLine2(2),'LineStyle','--','Marker','o', 'Color', 'b');
set(gca,'XTick',[1:1:10])
xlabel('persons')
ylabel(hAX(1),'weight') % left y-axis
ylabel(hAX(2),'height') % right y-axis
grid on;
axes(hAX(2)); hold on
text(7, 0.2, '* Batch 1', 'FontSize', 8); text(8.5, 0.2, 'o Batch 2', 'FontSize', 8);
plot([7 8], [0.15 0.15], 'b-'); plot([7 8], [0.1 0.1], 'b--');
text(8.5, 0.15, 'height', 'FontSize', 8);
text(8.5, 0.1, 'weight', 'FontSize', 8)

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 9 de Ag. de 2021
th(1) = plot(nan,nan,'*', 'displayname', 'Batch 1');
th(2) = plot(nan,nan,'o', 'displayname', 'Batch 2');
th(3) = plot(nan,nan,'-', 'displayname', 'height');
th(4) = plot(nan,nan,'--', 'displayname', 'weight');
legend(th, 'show')
  1 comentario
jaah navi
jaah navi el 9 de Ag. de 2021
when I run the command i am getting error stating Error using legend>process_inputs
Invalid argument. Type 'help legend' for more information.
Could you please help me on this.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by