One combined entry for multiple charts

For some reason, I would like to separately build a line and marker with the same data.

data1 = (1:1:10)';
data2 = (1:2:10);
figure(1);
plot(data1,data1,'or');
hold on;
plot(data2,data2,'-r');
legend('data');

However, it will only display the legend for the first plot. And Matlab doesn't seem to be able to manipulate marker, color and linestyle. enter image description here

How can I make such a legend?

enter image description here

Thanks!

+1
source share
1 answer

You will need to build a third invisible third plot (with almost no data to save it quickly) to determine your legend:

data1 = (1:1:10)';
data2 = (1:2:10);
figure(1);
plot(data1,data1,'or'); hold on
plot(data2,data2,'-r'); hold on

%// legend plot
lp = plot(0,0,'-r','Marker','o','visible','off')
legend(lp,'data');

enter image description here

You need to pass the handle of this invisible plot to the command legend, or you can even put the invisible plot in the legend:

legend(plot(0,0,'-r','Marker','o','visible','off'),'data');

,

style = @(LineStyle, MarkerStyle) plot(0,0,LineStyle,'Marker',MarkerStyle,'visible','off')
legend(style('-r','o'),'data');

... 'color', 'LineWidth' , .

:

legend([style('-r','o'),style('-b','x'),style('-g','v')],{'1','2','3'});

enter image description here

+5

Source: https://habr.com/ru/post/1608030/


All Articles