gwr pointed out an apparent problem with setting a default Option value of PlotLegends
for DateListPlot
. I traced the problem to the existence of PlotLegends -> "Expressions"
in the default PlotTheme
. This is true for ListPlot
as well. Since the explicit options within the Plot Theme override options set with SetOptions
it appears that our option is ignored.
Actually "Expressions"
is used across many Themes for ListPlot
:
PlotLegends /. Charting`ResolvePlotTheme["Default", ListPlot] & /@
{"Default", "Web", "Classic", "Detailed", "Scientific"}
{"Expressions", "Expressions", "Expressions", "Expressions", "Expressions"}
Why would this be? This option value doesn't seem to even work with these plot types, i.e.
dat = Table[{k, PDF[BinomialDistribution[50, p], k]}, {p, {0.3, 0.5, 0.8}}, {k, 0, 50}];
ListPlot[dat, Filling -> Axis, PlotLegends -> "Expressions"]
However it does cause the Themes to ignore a manually set default PlotLegends
value:
SetOptions[ListPlot, PlotLegends -> PointLegend[Automatic, {"one", "two", "three"}]];
ListPlot[dat, Filling -> Axis]
Observe that turning off the Plot Theme causes the legend to suddenly appear:
ListPlot[dat, Filling -> Axis, PlotTheme -> None]
Unless PlotLegends -> "Expressions"
has some desirable effect I have failed to observe this seems like plainly undesirable behavior. Was the inclusion of PlotLegends -> "Expressions"
in the Themes for ListPlot
and related functions a mistake?
(All observations made in Mathematica 10.1.0 under Windows.)
Answer
"Expressions" for legend in ListPlot family is used because of Association. With Association input, the plot will automatically pick up the keys as legends. Here is an example:
ListPlot[<|"A"->{1,2,3},"B"->{2,3,4}|>]
SetOptions is to set the default values to options but it doesn't guarantee that the settings will be used by the plot since PlotTheme has been introduced.
The workaround would be to pack a set of options you would like to use through out the session in a variable and use it instead of using SetOptions.
Comments
Post a Comment