22

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.

ArgentoSapiens
  • 7,780
  • 1
  • 32
  • 49
  • 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 02 '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 04 '13 at 04: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 04 '13 at 13:59

4 Answers4

17

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

Brett Champion
  • 20,779
  • 2
  • 64
  • 121
MinHsuan Peng
  • 2,046
  • 15
  • 14
  • 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 03 '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. – faysou Dec 03 '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 03 '12 at 23:09
  • @MinHsuanPeng, almost. I'm trying to set the default /for new notebooks/. – ArgentoSapiens Dec 04 '12 at 00:48
14

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.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • 1
    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 09 '13 at 16:30
13

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

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
  • 3
    -1 for Comic Sans.. – Brett Champion Dec 04 '12 at 04: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. – faysou Dec 04 '12 at 08:22
  • @FaysalAberkane Good idea. – Sjoerd C. de Vries Dec 04 '12 at 08:26
  • 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, – Benjamin May 09 '16 at 19:33
  • @benjamin As you could have read in the question PlotLegends is a V9 feature. – Sjoerd C. de Vries May 09 '16 at 19:36
  • I actually typed the code line Needs["PlotLegends`"] and then was able to use PlotLegend command. Do you have any recommendation for Mathematica 8 then? – Benjamin May 09 '16 at 19:57
  • I even tried this but still doesn't work: http://mathematica.stackexchange.com/questions/3433/formatting-legend-text-font – Benjamin May 09 '16 at 20:03
  • 1
    @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. – Sjoerd C. de Vries May 09 '16 at 20:08
11

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

Brett Champion
  • 20,779
  • 2
  • 64
  • 121
Rojo
  • 42,601
  • 7
  • 96
  • 188
  • Can you please clarify how that's done? This style does not appear in the Option Inspector. – ArgentoSapiens Dec 03 '12 at 23:00
  • @ArgentoSapiens, sure, give me a minute – Rojo Dec 03 '12 at 23:01
  • @ArgentoSapiens Style are to be found in the StyleSheet editor, not in the option inspector. – Sjoerd C. de Vries Dec 03 '12 at 23:16
  • @Rojo, thanks for explaining. How can this be applied to new notebooks? – ArgentoSapiens Dec 04 '12 at 00: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 04 '12 at 00:53
  • Creating a new stylesheet is just a matter of saving one of your modified stylesheet as a file, in one of the directories on the stylesheet path. Check CurrentValue[StyleSheetPath] – Rojo Dec 04 '12 at 00:55
  • @Rojo When I try this, the legend style does not inherit the FontFamily setting from the stylesheet. Size, weight, color all work, but the family remains "Times". – Michael E2 Feb 02 '13 at 20:12
  • @MichaelE2 you are right, it seems the FontFamily is hardcoded in the legends TemplateBox DisplayFunction. You could change the font family of the "Grid" style, but that would bring side-effects for the rest of the grids – Rojo Feb 02 '13 at 20:50