开发者

PlotLegends` default options

I'm trying to redefine an option of the PlotLegends package after having loaded it, but I get for example

Needs["PlotLegends`"]
SetOptions[ListPlot,LegendPosition->{0,0.5}]
=> SetOptions::optnf: LegendPosition is not a known optio开发者_开发知识库n for ListPlot.

I expect such a thing as the options in the PlotLegends package aren't built-in to Plot and ListPlot.

Is there a way to redefine the default options of the PlotLegends package?


The problem is not really in the defaults for PlotLegends`. To see it, you should inspect the ListPlot implementation:

In[28]:= Needs["PlotLegends`"]
In[50]:= DownValues[ListPlot]
Out[50]=    
{HoldPattern[ListPlot[PlotLegends`Private`a:PatternSequence[___,
    Except[_?OptionQ]]|PatternSequence[],PlotLegends`Private`opts__?OptionQ]]:>
  PlotLegends`Private`legendListPlot[ListPlot,PlotLegends`Private`a,
    PlotLegend/.Flatten[{PlotLegends`Private`opts}],PlotLegends`Private`opts] 
      /;!FreeQ[Flatten[{PlotLegends`Private`opts}],PlotLegend]}

What you see from here is that options must be passed explicitly for it to work, and moreover, PlotLegend option must be present.

One way to achieve what you want is to use my option configuration manager, which imitates global options by passing local ones. Here is a version where option-filtering is made optional:

ClearAll[setOptionConfiguration, getOptionConfiguration, withOptionConfiguration];
SetAttributes[withOptionConfiguration, HoldFirst];
Module[{optionConfiguration}, optionConfiguration[_][_] = {};
   setOptionConfiguration[f_, tag_, {opts___?OptionQ}, filterQ : (True | False) : True] :=
      optionConfiguration[f][tag] = 
         If[filterQ, FilterRules[{opts}, Options[f]], {opts}];
   getOptionConfiguration[f_, tag_] := optionConfiguration[f][tag];
   withOptionConfiguration[f_[args___], tag_] := 
        f[args, Sequence @@ optionConfiguration[f][tag]];
];

To use this, first define your configuration and a short-cut macro, as follows:

setOptionConfiguration[ListPlot,"myConfig", {LegendPosition -> {0.8, -0.8}}, False];
withMyConfig =   Function[code, withOptionConfiguration[code, "myConfig"], HoldAll];

Now, here you go:

withMyConfig[
   ListPlot[{#, Sin[#]} & /@ Range[0, 2 Pi, 0.1], PlotLegend -> {"sine"}]
]

PlotLegends` default options


LegendsPosition works in ListPlot without problems (for me at least). You don't happen to have forgotten to load the package by using Needs["PlotLegends"]`?


@Leonid, I added the possibility to setOptionConfiguration to set default options to f without having to use a short-cut macro.

I use the trick exposed by Alexey Popkov in What is in your Mathematica tool bag?

Example:

Needs["PlotLegends`"];
setOptionConfiguration[ListPlot, "myConfig", {LegendPosition -> {0.8, -0.8}},SetAsDefault -> True]
ListPlot[{#, Sin[#]} & /@ Range[0, 2 Pi, 0.1], PlotLegend -> {"sine"}]

Here is the implementation

Options[setOptionConfiguration] = {FilterQ -> False, SetAsDefault -> False};

setOptionConfiguration[f_, tag_, {opts___?OptionQ}, OptionsPattern[]] := 
    Module[{protectedFunction}, 

        optionConfiguration[f][tag] = 
            If[OptionValue@FilterQ, FilterRules[{opts}, 
                Options[f]]
                , 
                {opts}
            ];

        If[OptionValue@SetAsDefault, 
            If[(protectedFunction = MemberQ[Attributes[f], Protected]), 
                Unprotect[f];
            ];

            DownValues[f] = 
                Union[
                    {
                        (*I want this new rule to be the first in the DownValues of f*)
                        HoldPattern[f[args___]] :> 
                            Block[{$inF = True}, 
                                withOptionConfiguration[f[args], tag]
                            ] /; ! TrueQ[$inF]
                    }
                    , 
                    DownValues[f]
                ];

            If[protectedFunction, 
                Protect[f];
            ];
        ];
    ];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜