2
votes

The legend in my plot is too long, how can I break it into two lines? I'm plotting using plotyy and used this code:

leg = sprintf('I_{fitting}=I_0/(\surd{2\pi}\sigma)e^{-[t-t_0]^2/(2.\sigma^2)};\nso t_0=0.2\mus;\sigma=0.09\mus;I_0=4.8\cdot10^{10}W/cm^2') 
legend([h1 h2], 'I=10^{14}[T_e/(3680(1.054)^{1/3})]^{1.5}',leg); 

Can you tell me what the problem is?

1
Do you get any warnings when you plot? Are you changing the Interpreter? Please provide an example where you actually call plotyy and determine h1 and h2. - horchler
please look at on my previous question, I am trying to make this kind of broken legend there [link]{stackoverflow.com/questions/21862835/…} - user3271929
Please edit this question to be clear about the exact nature of the problem and provide a simple runnable example that demonstrates it. StackOverflow works best when you do a bit of work to help us help you. - horchler

1 Answers

5
votes

Try this

>> leg = sprintf('This is a really long line\nso I broke it in two')
>> plot(1:10, 1:10)
>> legend(leg)

Which results in

enter image description here

This won't work if there are other characters in your legend that need to be escaped (e.g. if there is a lot of LaTeX). In that case you can insert a newline character manually -

>> newline = char(10);
>> leg = ['This is the first line', newline, 'and the second: A = 2\pi r^2'];
>> plot(1:10, 1:10);
>> legend(leg)

which results in

enter image description here