Matlab two different colors in one line in the legend

In Matlab, the following generates black in the specified string 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 one line in a legend. (therefore, in this example, the 'test' part should be black and the 'left y-axis' part should be red.

What I tried:

  • Execution of the above: no result.
  • Getting string arguments from a legend and changing color: the color of the entire string.
  • Using LaTeX to color text: no result.

Image for visualizing what I mean:

enter image description here

+4
source share
2 answers

You probably mistype the tex line:

 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{:}) 

Forms:

Colored legend

+6
source

I'm going to be stupid and guess that you made a mistake.

Here we see that latex should work on drawing labels .

And here we see that matlab should allow the use of colors with latex .

Unfortunately, I have no way to try it now, but I would suggest that this is the way to go. If this fails, please show which code you used.

+3
source

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


All Articles