Save plot options in list - wolfram-mathematica

I wonder if it is possible to make a variable that holds info obout wanted style for Show[] function. So ewery time i want to display plots in Show[] i will insert that variable in Show[] to set options for style.
I want something like this...
OPTIONS=
AxesOrigin -> Automatic,
LabelStyle -> Directive[14, Black, Bold, Italic],
ImageSize -> {450}
Show[
ListLinePlot[data],
OPTIONS
]
The solution is simple but im green. :)

OPTIONS = {AxesOrigin -> Automatic,
LabelStyle -> Directive[14, Red, Bold, Italic], ImageSize -> {450}}
Show[ListLinePlot[{1, 2, 3, 4, 5}], OPTIONS]
Works for me.

You could apply Show with options to graphics automatically using $Post, e.g.
$Post := With[{opts = {
AxesOrigin -> Automatic,
LabelStyle -> Directive[14, Black, Bold, Italic],
ImageSize -> {250}}},
If[Head[#] === Graphics, Show[#, opts], #] &]
ListLinePlot[{1, 2, 3, 4, 5}]
Restoring $Post to default:
$Post =.

Related

Plot error band using functional form

I have a data set with x,y and error(y) values. I write this in mathematica as:
Needs["ErrorBarPlots`"]
data = {{{0, 0.10981309359605919},
ErrorBar[0.05240427422664753`]}, {{0.2145, 0.09146326059113304},
ErrorBar[0.034195343626358385`]}, {{0.4290, 0.08230438177339898},
ErrorBar[0.02533205817067696`]}, {{0.6435, 0.0768141842364532},
ErrorBar[0.020205473852635995`]}, {{0.8580, 0.07223473349753692},
ErrorBar[0.016156209168991867`]}, {{4, 0.056122650246305375},
ErrorBar[0.009288720442961331]}};
ErrorListPlot[data, Frame -> True, FrameStyle -> Directive[Black, 20],
PlotRange -> {{-0.1, 5}, {0.2, 0}}, Axes -> False,
PlotStyle -> {Directive[Red, 12], AbsolutePointSize[10],
AbsoluteThickness[3]} , LabelStyle -> Directive[Green],
BaseStyle -> {Large, FontFamily -> "Courier", FontSize -> 12}]
But what I am trying to obtain is draw a line and get a shaded error band connecting the errorbars which obey a functional form, f(x)= 0.05 + 0.02/(x^2 + 0.425) . I don't want to show the error bars explicitly , rather I want to show the band. I am looking for something like this
I have looked at this link http://reference.wolfram.com/language/howto/GetResultsForFittedModels.html
but couldn't solve the problem. Could anyone please help me? Thanks.
Here is one approach, make two lists, one list for upper range of the erros:
dataPLUS = {{0, 0.10981309359605919 + 0.05240427422664753`}, {0.2145,
0.09146326059113304 + 0.034195343626358385`}, {0.4290,
0.08230438177339898 + 0.02533205817067696`}, {0.6435,
0.0768141842364532 + 0.020205473852635995`}, {0.8580,
0.07223473349753692 + 0.016156209168991867`}, {4,
0.056122650246305375 + 0.009288720442961331}};
another list for the lower range of the errors as:
dataMINUS = {{0, 0.10981309359605919 - 0.05240427422664753`}, {0.2145,
0.09146326059113304 - 0.034195343626358385`}, {0.4290,
0.08230438177339898 - 0.02533205817067696`}, {0.6435,
0.0768141842364532 - 0.020205473852635995`}, {0.8580,
0.07223473349753692 - 0.016156209168991867`}, {4,
0.056122650246305375 - 0.009288720442961331}};
Once you have the two sets you can use the ListPlot option as:
ListPlot[{dataPLUS, dataMINUS}, PlotStyle -> Red, PlotRange -> All]
which will generate a graph like
if you want to join them, instead use ListLinePlot option
ListLinePlot[{dataPLUS, dataMINUS}, PlotStyle -> Red,PlotRange -> All]
and to have a shaded region in between, use the Filling option
ListLinePlot[{dataPLUS, dataMINUS}, PlotStyle -> Red, Filling -> {1 -> {{2}, Gray}}, PlotRange -> All]
To get smooth graph, you need more data points. Hope this will help.
And to include the BestFit line, define a function and add to the previous plots as:
f[x_] = 0.05 + 0.02/(x^2 + 0.425);
plot2 = Plot[f[x], {x, 0, 5}, PlotStyle -> {Red, Thick}];
plot1 = ListLinePlot[{dataPLUS, dataMINUS}, PlotStyle -> LightGray,Filling -> {1 -> {{2}, LightGray}}, PlotRange -> All];
Show[{plot1, plot2}]

How to generate function name automatically in mathematica?

When I draw multiple functions like exp,2^x,3^x, is it possible to generate a label of each function?
My code now:
Plot[{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, PlotStyle -> {Red, Green, Blue}]
What I mean is generate 3 labels in this case to tell the user what function it is.
Such as:
How do you generate this?
Perhaps this works: Use Tooltip in Plot to generate a Graphics object with tooltips. Then rewrite the tooltip to place the desired text in the desired location:
Plot[
Tooltip#{Exp[x], 2^x, 3^x}, {x, -5, 2},
AspectRatio -> Automatic,
PlotStyle -> {Red, Green, Blue},
PlotRange -> All,
PlotRangePadding -> 1.1] /.
{
Tooltip[{_, color_, line_}, tip_]
:>
{Text[Style[tip, 14], {.25, 0} + line[[1, -1]]], color, line}
}
I am not sure what the rules are for adding another, different answer for the same question. But here is another, different way to do it. If I am supposed to add this to my first answer, I can do that.
You can add the text labels, by hand, using Text commands. I think it looks better. Here is one way:
Clear[x];
funs = {Exp[x], 2^x, 3^x};
funNames = Style[#, 12] & /# funs;
(*the x-axis plot range used *)
from = -5; to = 2;
(* generate the coordinates at the end of the plot lines*)
pos = Map[{to, #} &, funs /. x -> to];
(*generate the text labels *)
text = Map[Text[#[[1]], #[[2]], {-1, 0}] &, Thread[{funNames, pos}]];
Plot the final result (added a little of padding to plot range so that
the labels added are seen completely)
Plot[funs, {x, from, to},
PlotRangePadding -> {1, 1},
PlotStyle -> {Red, Green, Blue},
PlotRange -> All,
Epilog -> text
]
update (1)
Sam asked below for an simpler way. I am not sure now. But one way to make it easier to use this method, is to make a function and then simply call this function once to generate the Text labels. You can put this function where you put all your other functions you use all the time, and just call it.
Here is something: First write the function
(*version 1.1*)
myLegend[funs_List, (*list of functions to plot*)
x_, (*the independent variable*)
from_?(NumericQ[#] && Im[#] == 0 &),(*the x-axis starting plot range*)
to_?(NumericQ[#] && Im[#] == 0 &) (*the x-axis ending plot range*)
] := Module[{funNames, pos, text, labelOffset = -1.3},
(*make label names*)
funNames = Style[#, 12] & /# funs;
(*generated the coordinates at the end of the plot lines*)
pos = Map[{to, #} &, funs /. x -> to];
(*generate the Text calls*)
text = Map[Text[#[[1]], #[[2]], {labelOffset, 0}] &,
Thread[{funNames, pos}]]
];
And now just call the above any time you want to plot with labels. It will be just 1-2 extra lines of code. like this:
Clear[x]
from = -5; to = 2;
funs = {Exp[x], 2^x, 3^x};
Plot[funs, {x, from, to}, PlotRangePadding -> {1, 1},
PlotStyle -> {Red, Green, Blue}, PlotRange -> All,
Epilog -> myLegend[funs, x, from, to]]
Here are few examples:
You can modify it as you want.
Alternative way with Tooltip displaying labels while the mouse pointer is at the function graphs :
Plot[Tooltip#{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic,
PlotStyle -> {Red, Green, Blue}]
One way is to use PlotLegends
(I do not like it too much, but it is an easy way to do what you want)
<< PlotLegends`
Clear[x];
funs = {Exp[x], 2^x, 3^x};
legends = Map[Text#Style[#, "TR", 12] &, funs];
Plot[Evaluate#funs, {x, -5, 2}, AspectRatio -> Automatic,
PlotStyle -> {Red, Green, Blue}, PlotLegend -> legends]
see help to customize the legend more. The above uses defaults.
http://reference.wolfram.com/mathematica/PlotLegends/tutorial/PlotLegends.html

Style dynamic value

Can I apply Style to the output of "Labeled" in the below ?
Manipulate[\[Lambda],
Control#{{\[Lambda], 401,
Style[" \[Lambda]", Black, Bold, 24]},
Range[401, 570, 1],
ControlType -> Slider,
ControlPlacement -> Bottom,
Appearance -> "Labeled",
ImageSize -> 200}]
That is on the right part of the Slider :
You want the option BaseStyle (which appears in Options[Slider]). E.g.
Manipulate[
Plot[Cos[k x], {x, 0, 2 Pi}, PlotLabel -> "Cosine"],
{{k, 1, Style["x", Black, Bold, 24]}, 0, 4,
ControlType -> Slider, Appearance -> "Labeled",
ControlPlacement -> Bottom, ImageSize -> 200,
BaseStyle -> {Red, Large, Italic, FontFamily -> "Times"}}]
When looking at this I noticed that you can also use the almost undocumented ControlType -> LabeledSlider, just for something different.
It seems to be at least partially affected by LabelStyle and BaseStyle. (I'm having trouble changing the font, for some reason, but size, weight, color seem to work fine.)

Adjust Font using LabelingFunction in a BarChart in Mathematica

Considering the following BarChart :
How Could I adjust the Font for the Labeling Function as well as the Chart Labels ?
I tried nesting Style. Unsuccessfull.
BarChart[{1, 2, 3, 4},
ChartStyle -> {Blue, Red, Green, Yellow},
LabelStyle -> Directive[Black, Large],
ChartLabels -> {"COG1", "COG2", "COG3", "COG4"},
ImageSize -> {500, 300},
ChartBaseStyle -> EdgeForm[Thick],
LabelingFunction -> Bottom,
Background -> Black,
LabelStyle -> Directive[Black, Large]]
Do you mean something like this?
BarChart[{1, 2, 3, 4},
ChartStyle -> {Blue, Red, Green, Yellow},
ChartLabels -> (Style[#, Large, White] & /# {"COG1", "COG2", "COG3", "COG4"}),
LabelingFunction -> (Placed[Style[#, Large], Bottom] &),
ImageSize -> {500, 300},
ChartBaseStyle -> EdgeForm[Thick],
Background -> Black,
AxesStyle -> White, LabelStyle -> {Large}]
Edit
Change the style of the axes.

Histogram plots in LevelScheme

I've just started using LevelScheme, and have issues with getting the histogram to fit correctly within the figure. A minimal non-working example:
<<"LevelScheme`"
Figure[{FigurePanel[{{0, 1}, {0, 1}},
LabB -> textit["x"], BufferB -> 2.5,
LabL -> textit["p(x)"], BufferL -> 2.5,
FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
PlotRange -> {{-3, 3}, {0, 0.5}}],
RawGraphics[
Histogram[RandomReal[NormalDistribution[], 1000], Automatic,
"ProbabilityDensity"]]},
Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]
The output looks like this
when it should look like this
Basically, the Histogram graphics object doesn't obey the FigurePanel's PlotRange, but instead obeys the main Figure's PlotRange. This behaviour doesn't occur when the Histogram is replaced by a Plot or similar commands. So the following produces a clean plot
Figure[{FigurePanel[{{0, 1}, {0, 1}},
LabB -> textit["x"], BufferB -> 2.5,
LabL -> textit["p(x)"], BufferL -> 2.5,
FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
PlotRange -> {{-3, 3}, {0, 0.5}}],
RawGraphics[Plot[1/Sqrt[2 Pi] Exp[-x^2/2], {x, -4, 4}]]},
Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]
Has anyone else encountered this issue? Or, do you have suggestions for a fix?
EDIT
I thought I'd add some green to the question. I'm still interested in knowing how to overcome this hurdle.
Well, I recon you won't like this one too much but it is a workaround of sorts.
If I give PerformanceGoal -> "Speed" as a Histogram option (rather than PerformanceGoal -> "Quality") I disable interactive behaviour but, with a few minor tweaks, I get the following:
<< "LevelScheme`"
Figure[{FigurePanel[{{0, 1}, {0, 1}}, LabB -> textit["x"],
BufferB -> 2.5, LabL -> textit["p(x)"], BufferL -> 2.5,
FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
PlotRange -> {{-3, 3}, {0, 0.55}}],
RawGraphics[
Histogram[RandomReal[NormalDistribution[], 1000], Automatic,
"ProbabilityDensity", PerformanceGoal -> "Speed"]]},
Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.15, 1.1}}]
As Simon mentioned in a comment, you can use LevelScheme's DataPlot to plot a histogram.
<< "LevelScheme`"
histData[x_] :=
Cases[x, RectangleBox[{bl_, _}, {br_, c_}] :> {{bl, br}, c},
Infinity];
hist = histData[
Histogram[RandomReal[NormalDistribution[], 1000], {-4, 4, 0.1},
"ProbabilityDensity"]];
bins = hist[[All, 1, 1]]; counts = hist[[All, 2]];
data = Table[{bins[[i]], counts[[i]]}, {i, 1, Length#counts}];
Figure[{FigurePanel[{{0, 1}, {0, 1}}, LabB -> textit["x"],
BufferB -> 2.5, LabL -> textit["p(x)"], BufferL -> 2.5,
FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
PlotRange -> {{-3, 3}, {0, 0.5}}],
DataPlot[data,
DataLine -> {LineShape -> "Histogram", LineColor -> Darker#Blue},
DataSymbol -> {SymbolSize -> 0.00001}],
RawGraphics[
Plot[1/Sqrt[2 Pi] Exp[-x^2/2], {x, -4, 4},
PlotStyle -> {Red, Thick}]]
}, Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]
However, I haven't managed to get filled histogram bars like that produced by Histogram or BarChart, if that was also what you had intended.
BTW, the function histData is similar to something I saw on a mathematica help forum long ago, and it went in my useful functions toolkit. I don't remember where I read that or when, to credit it. However, it is not all that of a magic function now to me, as it was back then.
I know what the problem is, but I don't have an immediate fix. The way LevelScheme works is that it transforms the Graphics objects so that they fit correctly. To do this, RawGraphics uses the legacy function TransformGraphics from LegacyPackages\Graphics\Graphics.m which is included in LegacyTransformGraphics.m in v. 3.51 of the LevelScheme packages. Looking at the FullForm of your Histogram, you can see that TransformGraphics knows nothing about dealing with the sort of objects produced. Mark Caprio is working on an update to LevelScheme over the next couple of months, so there may be a fix on the way. In the mean time, try using Rasterize before supply your histogram to RawGraphics, although it may not give you good results.
Edit:
Instead of using the legacy version of TransformGraphics, a more recent version might look like
TransformGraphics[
(g:(Graphics | Graphics3D))[prims__, opts:OptionsPattern[], transform_]:=
g[ GeometricTransformation[prims, transform], opts ]
Of course, the trick is now supplying a version of transform that GeometricTransformation can accept. Although, the legacy TransformGraphics, applies a function, its second argument, directly to the points found in g, so using the above code may work without any additional changes.
To try it, replace Needs["LevelScheme`LegacyTransformGraphics`"] with the above code in either LevelScheme.nb (and regenerate LevelScheme.m) or in LevelScheme.m directly. It may not work completely, as I don't see where the options are substituted, but it should be a start.

Resources