9
$\begingroup$

Searching got me peripherally related threads, but not exactly what I was looking for.

I'm making some graphs which I want to use on exams. Sometimes, labels (specifically in AxesLabel and PlotLabel) that are longish can be a hassle. Is there a way to introduce a line break in these labels to make them fit better in the space that I have? Thanks in advance.

$\endgroup$
1
  • $\begingroup$ I get the feeling this question is probably a duplicate $\endgroup$ Jul 25, 2014 at 3:47

2 Answers 2

18
$\begingroup$

Try this:

Plot[Sin[2 x], {x, -Pi, Pi}, 
  AxesLabel -> {"This is\n an axes label", None}]

enter image description here

And here's the same using FrameLabel instead of AxesLabel.

Plot[Cos[2 x], {x, -Pi, Pi}, Frame -> True, 
 FrameLabel -> {{"This is\n a y frame label",None}, 
 {"This is\n an x frame label", None}}]

enter image description here

This is covered in the documentation under Newlines and Tabs in Strings.

And if you want more control, you can even use Columns and Grids:

Plot[Cos[2 x], {x, -Pi, Pi}, Frame -> True, 
 FrameLabel -> {{Column[{"Column1", "Column2"}], None},
 {Grid@{{1, 2}, {3, 4}}, None}}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ I tried \n but it just shows up in the label that way, no line break. Am I missing something? $\endgroup$ Jul 25, 2014 at 19:06
  • 1
    $\begingroup$ I notice that imposing formatting on the text seems to be the cause. But thanks! $\endgroup$ Jul 25, 2014 at 19:12
5
$\begingroup$

An alternative to manually setting line breaks is to set a label size and then let the label break as needed:

Plot[Cos[2 x], {x, -Pi, Pi}, Frame -> True, 
 FrameLabel -> {{Pane["This is a y frame label", {50, All}], 
    None}, {Pane["This is an x frame label", {50, All}], None}}] 

enter image description here

$\endgroup$
3
  • $\begingroup$ Why 50 and not 45 (which gives the same result) ? Why 45 and not 20 ? $\endgroup$
    – eldo
    Jul 25, 2014 at 0:51
  • 1
    $\begingroup$ @eldo why not 55, why not 150, why not 126, why not 87. the whole point is to choose whatever label size you want. If you like 20 then choose 20. $\endgroup$ Jul 25, 2014 at 1:11
  • $\begingroup$ BTW I think this Q&A is probably a duplicate $\endgroup$ Jul 25, 2014 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.