Inserting formula into plot - wolfram-mathematica

What's a standard way to insert formula into Plot?
My plot is below. I'd like to have the formula of the plot nicely formatted and inserted into plot. TraditionalForm looks OK, but it puts the formula in one line, whereas I want a two-line fraction.
Plot[{1, (\[CapitalDelta] - 1)^(\[CapitalDelta] -
1)/(\[CapitalDelta] - 2)^\[CapitalDelta]}, {\[CapitalDelta], 3, 6},
PlotRange -> {0, 4}, PlotStyle -> {Dashing[.02], Thick},
AxesLabel -> {"\[CapitalDelta]", "\[Lambda]"}]

Sorry for being late :D. I mostly use a simplified version of Leonid's answer. Not sure if it is general enough for any purpose, but certainly works here.
Plot[{1, (\[CapitalDelta] - 1)^(\[CapitalDelta] -
1)/(\[CapitalDelta] - 2)^\[CapitalDelta]}, {\[CapitalDelta], 3,
6}, PlotRange -> {0, 4}, PlotStyle -> {Dashing[.02], Thick},
AxesLabel -> {"\[CapitalDelta]", "\[Lambda]"},
Epilog -> Inset[HoldForm#TraditionalForm[
(\[CapitalDelta] - 1)^(\[CapitalDelta] - 1)/
(\[CapitalDelta] - 2)^\[CapitalDelta]]]]
Or use Epilog -> Inset[Panel#HoldForm#TraditionalForm... for a nice box around the function:

Perhaps this could get you started?
Plot[{1,(\[CapitalDelta]-1)^(\[CapitalDelta]-1)/(\[CapitalDelta]-2)^\[CapitalDelta]},
{\[CapitalDelta],3,6},PlotRange->{0,4},
PlotStyle->{Dashing[.02],Thick},AxesLabel->{"\[CapitalDelta]","\[Lambda]"},
Epilog->Inset[Style[
HoldForm##MakeExpression#MakeBoxes#TraditionalForm[(\[CapitalDelta]-1)^
(\[CapitalDelta]-1)/(\[CapitalDelta]-2)^\[CapitalDelta]],10]]]

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}]

Plotlegend in Mathematica does not work for a table of functions

I am trying to plot a list of functions with a legend using PlotLegend in Mathematica v8. As a simple test illustrating what I'm trying to do.
<<PlotLegends`
test = Table[f[x], {f, {Sin, Cos, Tan, Log, Exp}}]
Plot[test, {x, 0, 1}, PlotRange -> Full, Axes -> {True, False},
PlotStyle -> Thick, AxesOrigin -> {0, 0},
PlotLegend -> {"Sin", "Cos", "Tan", "Log", "Exp"},
LegendPosition -> {0, -0.5}, LegendShadow -> None]
Gives as output
{Sin[x], Cos[x], Tan[x], Log[x], E^x}
However, if I explicitly put the table in the Plot command, I get the correct legend.
Plot[{Sin[x], Cos[x], Tan[x], Log[x], Exp[x]}, {x, 0, 1},
PlotRange -> Full, Axes -> {True, False}, PlotStyle -> Thick,
AxesOrigin -> {0, 0},
PlotLegend -> {"Sin", "Cos", "Tan", "Log", "Exp"},
LegendPosition -> {0, -0.5}, LegendShadow -> None]
For my actual application, I'm putting together a list of functions within a Do loop, so the latter Plot command is not ideal.
Any suggestions would be greatly appreciated.
Cheers,
Mike
Replace Plot[test, ...] with Plot[Evaluate#test, ...].
The problem is that Plot takes the first argument unevaluated and only evaluates it when calculating points. Therefore when it determines the labels, it only sees a single argument test, not a list, and therefore it only outputs one label. Evaluate#test tells Mathematica to evaluate test before passing it to Plot even though Plot is defined to take the argument unevaluated. This way, Plot sees the list you stored in test and knows to generate several labels.

Mathematica: Unwanted vertical line in histogram

When changing the plot range of a histogram from Automatic to a plot range which exceeds the automatically calculated plot range, Mathematica draws an unwanted horizontal line which I cannot get rid of (see right histogram at value -4). Does anyone have a suggestion on that issue?
I'm running Mathematica V.8.0.1.0 on Mac OS 10.7.2.
In[1099]:=
data = {-1.2056, -1.46192, -1.30053, -2.52879, -0.99636, -1.73904, -1.164,
-1.83398,-0.97505, -0.503256, -0.63802, -0.785963, -0.711821, -0.820439, -1.8699,
-3.9659, -1.4456, -1.67021, -1.42009, -2.5644, -1.45002, -1.27806, -1.66529,
-1.67073, -3.31102, -3.38638};
HistogramLeft=Histogram[data, PlotRange -> Automatic]
HistogramRight=Histogram[data, PlotRange -> {-8, 0}]
It looks like you're using Frame -> {{True, False}, {True, False}} (or something similar.) If that's the case, you can turn off the normal axes entirely with Axes->False.
Look at the option AxesOrigin.
HistogramRight = Histogram[data, PlotRange -> {-8, 0}, BarOrigin -> Left, AxesOrigin -> {-8, 0}]
If I understand what you want, this is pretty easy, I think.
Try
HistogramRight =
Histogram[data, PlotRange -> {-8, 0}, BarOrigin -> Left,
PlotLabel -> Left, Axes -> {False, True}]
By the way, there is a new stack exchange for Mathematica at https://mathematica.stackexchange.com/

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.

How do I control the appearance of a Locator inside a Mathematica's Manipulate statement?

If I have a Manipulate statement, such as:
Manipulate[
Graphics[Line[{{0, 0}, pt}], PlotRange -> 2], {{pt, {1, 1}},
Locator}]
How do I change the appearance of the Locator object in the easiest way possible? Do I have to resort to Dynamic statements? Specifically, I would have liked to make the Locator invisible.
In addition to WReach's answer: In a normal Locator call its appearance can be given as one of the arguments. When used in a Manipulate this is not possible. However, Appearance can be used to draw other locator symbols.
a = Graphics[{Red, Table[Circle[{0, 0}, i], {i, 3}]}, ImageSize -> 20];
Manipulate[
Graphics[Line[{{0, 0}, pt}], PlotRange -> 2], {{pt, {1, 1}}, Locator,
Appearance -> a}]
I don't think this is documented. Last year I tried finding out how to do this, but couldn't find a way. Got no response on my question on the mathematica newsgroup either.
Try adding Appearance -> None to the Locator control:
Manipulate[
Graphics[
Line[{{0, 0}, pt}]
, PlotRange -> 2
]
, {{pt, {1, 1}}, Locator, Appearance -> None}
]

Resources