Question

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

Était-ce utile?

La solution

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

Autres conseils

I am going to be blunt and guess that you made a mistake.

Here we can see that latex should work in figure labels.

And here we can see that matlab should allow using colors with latex.

Unfortunately I don't have the chance to try it out now, but I would guess this is the way to go. If it fails, please show what code you used.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top