22
$\begingroup$

I'm pleased that Mathematica 9 appears to have brought us prettier legends. If I always want the legend in

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> {"one", "two"}]

enter image description here

to appear in Helvetica, how can I set this as the default for new notebooks? Here you may notice I have already discovered how to change the default font in the GraphicsBoxOptions BaseStyle. Is there a similar way to affect PlotLegends?


I thank answerers for their contributions so far, but nobody has showed what to change to make this the default for all new notebooks.

$\endgroup$
3
  • $\begingroup$ By "new" I take it you want old notebooks to use the regular style, and not use Helvetica. Only newly created notebooks are to use Helvetica. Is that right? And do you want the option set only for notebooks you use on your computer, or should the new default go with the notebook if you, say, email it someone? $\endgroup$
    – Michael E2
    Feb 2, 2013 at 20:08
  • $\begingroup$ Thanks for asking for clarification. By "new" I only mean that there should be zero clicks or keystrokes between creating a new document and making a plot with the desired default font in the legend. Behavior of old notebooks need not be preserved and behavior on other people's computers doesn't matter. $\endgroup$ Feb 4, 2013 at 4:12
  • $\begingroup$ I cannot play around with these new PlotLegends in version 7 but I expect that you could use a method similar to that described in my answer to: How to change the default ColorData used in Mathematica's Plot? $\endgroup$
    – Mr.Wizard
    Feb 4, 2013 at 13:59

4 Answers 4

14
+50
$\begingroup$

You can put the SetOptions instruction into your init.m file and then it will always be used. Just evaluate this once and you are all set:

(Export[#, 
    Import[#, "Text"] <> 
     "\n\nSetOptions[LineLegend,LabelStyle\[Rule]{FontFamily\[Rule]\"\
Comic Sans MS\",Red}]", "Text", 
    CharacterEncoding :> $CharacterEncoding]) &@FindFile["init.m"]

Notice that without CharacterEncoding :> $CharacterEncoding things go wrong for \[Rule]. I find it strange that one has to use this semi-undocumented option and that it is not set in this way by default.

$\endgroup$
1
  • 1
    $\begingroup$ Is there a way to change the FontWeight this way as well? I've attempted to add the following to my init.m: SetOptions[LineLegend,LabelStyle->{FontFamily -> "Arial",FontWeight -> "Bold",FontSize -> "Medium"}], but the only thing it changes is the font to Arial. There is no change to the weight or the size. $\endgroup$
    – jat255
    Sep 9, 2013 at 16:30
17
$\begingroup$

There is LabelStyle option for these new legends.

For example:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, 
 PlotLegends -> 
  LineLegend["Expressions", 
   LabelStyle -> {FontFamily -> "Helvetica"}]]

labelstyle

$\endgroup$
4
  • $\begingroup$ Thanks for your quick answer. But I am interested in changing the /default/ font, not the font of the individual legend instance. Do you know how that can be done? $\endgroup$ Dec 3, 2012 at 23:00
  • 1
    $\begingroup$ SetOptions[LineLegend,LabelStyle->{FontFamily -> "Helvetica"}], the problem is that if you use different options for LabelStyle (ie not FontFamily), the default font won't be there anymore. $\endgroup$
    – faysou
    Dec 3, 2012 at 23:09
  • $\begingroup$ I think this is probably not better than Rojo's solution of editting Legended style in stylesheet. But will this suffice the need? SetOptions[LineLegend, LabelStyle -> {FontFamily -> "Helvetica"}] $\endgroup$ Dec 3, 2012 at 23:09
  • $\begingroup$ @MinHsuanPeng, almost. I'm trying to set the default /for new notebooks/. $\endgroup$ Dec 4, 2012 at 0:48
13
$\begingroup$

Perhaps just defining a custom legend name:

myStyle[labels_, opts : OptionsPattern[]] := 
 LineLegend[labels, opts, 
  LabelStyle -> {FontFamily -> "Arial", FontSize -> 20}, LegendFunction -> "Frame", 
  Background -> Directive[LightGray, Opacity[0.3]]
]

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]},  PlotLegends -> myStyle[{"one", "two"}]]

Mathematica graphics

This base style can be easily overruled by specific adjustments:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, 
  PlotLegends -> myStyle[{"one", "two"}, LabelStyle -> {FontFamily -> "Comic Sans MS", Red}]]

Mathematica graphics

$\endgroup$
8
  • 3
    $\begingroup$ -1 for Comic Sans.. $\endgroup$ Dec 4, 2012 at 4:11
  • $\begingroup$ +1, having the definition myStyle[labels_: "Expressions", opts : OptionsPattern[]] and doing SetOptions[Plot, PlotLegends -> myStyle[]] allows to have nice default legends while retaining the possibility to alter them. $\endgroup$
    – faysou
    Dec 4, 2012 at 8:22
  • $\begingroup$ @FaysalAberkane Good idea. $\endgroup$ Dec 4, 2012 at 8:26
  • $\begingroup$ Hi dear Sjored, Thanks for this response. I am trying to apply this to my plot on Mathematica 8 and it is not working. It runs but it has no effect on the size of the fonts inside my legend. Do you know what could have gone wrong? Thanks, $\endgroup$
    – Benjamin
    May 9, 2016 at 19:33
  • 1
    $\begingroup$ @Benjamin the PlotLegends package is totally unrelated to the PlotLegends v9+ builtin function, but there are many questions about it on this site. You may want to search on these first, and if that fails ask a new question. $\endgroup$ May 9, 2016 at 20:08
11
$\begingroup$

You can modify the "Legended" style

A simple way to accomplish this for a particular notebook is to Format->Edit Stylesheet

Then in the InputField on top, you write Legended and press enter. A style cell will appear in the stylesheet. You then select that cell, go to the Options Inspector and modify the style at will.

enter image description here

$\endgroup$
8
  • $\begingroup$ Can you please clarify how that's done? This style does not appear in the Option Inspector. $\endgroup$ Dec 3, 2012 at 23:00
  • $\begingroup$ @ArgentoSapiens, sure, give me a minute $\endgroup$
    – Rojo
    Dec 3, 2012 at 23:01
  • $\begingroup$ @ArgentoSapiens Style are to be found in the StyleSheet editor, not in the option inspector. $\endgroup$ Dec 3, 2012 at 23:16
  • $\begingroup$ @Rojo, thanks for explaining. How can this be applied to new notebooks? $\endgroup$ Dec 4, 2012 at 0:49
  • $\begingroup$ @ArgentoSapiens. There are ways to make it a global default, but what I'd do is create a new stylesheet, make it inherit from the default stylesheet and add those definitions. Then you can just choose that stylesheet for your notebooks $\endgroup$
    – Rojo
    Dec 4, 2012 at 0:53

Your Answer

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

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