Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab two different colors in same line in legend

In Matlab, the following generates a black color on the specified line in the legend:

leftAxis = sprintf('left y-axis','Color','r');
leg = legend([.. bla bla ..], sprintf('test [%s]', leftAxis), etc... );

What I'm trying to achieve is two colors on the same line in the legend. (so in this example the part 'test' should be black and the part 'left y-axis' should be red.

What I've tried:

  • Doing the above: no result.
  • Getting the string arguments from the legend and altering the color: colors the whole line.
  • Using LaTeX to color the text: no result.

A picture to visualise what I mean:

enter image description here

like image 806
Jean-Paul Avatar asked Jan 13 '23 09:01

Jean-Paul


1 Answers

Probably you mistyped tex string:

figure
hold on
line1H=plot(1:10,1:10);
line2H=plot(1:10,2*(1:10),'r');

leg{1} = 'BlackText {\color{blue}line1} BlackAgain';
leg{2} = 'BlackText {\color{red}line2} BlackAgain';

legend([line1H,line2H],leg{:})

Generates:

Colored Legend

like image 55
Werner Avatar answered Jan 19 '23 07:01

Werner