Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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? –  Michael E2 Feb 2 '13 at 20:08
    
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. –  ArgentoSapiens Feb 4 '13 at 4:12
    
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? –  Mr.Wizard Feb 4 '13 at 13:59
add comment

4 Answers

up vote 10 down vote accepted
+50

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.

share|improve this answer
    
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. –  jat255 Sep 9 '13 at 16:30
add comment

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

share|improve this answer
    
Can you please clarify how that's done? This style does not appear in the Option Inspector. –  ArgentoSapiens Dec 3 '12 at 23:00
    
@ArgentoSapiens, sure, give me a minute –  Rojo Dec 3 '12 at 23:01
    
@ArgentoSapiens Style are to be found in the StyleSheet editor, not in the option inspector. –  Sjoerd C. de Vries Dec 3 '12 at 23:16
    
@Rojo, thanks for explaining. How can this be applied to new notebooks? –  ArgentoSapiens Dec 4 '12 at 0:49
    
@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 –  Rojo Dec 4 '12 at 0:53
show 3 more comments

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

share|improve this answer
    
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? –  ArgentoSapiens Dec 3 '12 at 23:00
1  
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. –  Faysal Aberkane Dec 3 '12 at 23:09
    
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"}] –  MinHsuan Peng Dec 3 '12 at 23:09
    
@MinHsuanPeng, almost. I'm trying to set the default /for new notebooks/. –  ArgentoSapiens Dec 4 '12 at 0:48
add comment

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

share|improve this answer
2  
-1 for Comic Sans.. –  Brett Champion Dec 4 '12 at 4:11
    
+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. –  Faysal Aberkane Dec 4 '12 at 8:22
    
@FaysalAberkane Good idea. –  Sjoerd C. de Vries Dec 4 '12 at 8:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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