# Plot Styling Errors (PlotLabel, PlotTheme, Inset not appearing)

Question:

I'm trying to pull together a graph with labelled axes, a title, and an inset box with information relating to the plot such as the y-intercept (A in this code) and it's uncertainty. However, none of the conditions beyond the initial plot implements, and outputs 3 or 4 pages of errors, but can't see what the error is, even with the description

Code:

fit = NonlinearModelFit[dataHist5, A*Exp[-k*t], {A, k}, t,
Weights -> 1/uncertainties^2];
hLife = Log[2]/Around[k, \[Sigma]k]
seA = Around[A, \[Sigma]A]

halfLife = hLife[[1]] /. fit["BestFitParameters"]
sehalfLife =
hLife[[2]] /. fit["BestFitParameters"] /. \[Sigma]k ->
fit["ParameterErrors"][[2]]

uncertainties = dataHist5[[All, 2, 2]];

Show[ListPlot[dataHist5],
Plot[fit[x], {x, 0, 800}, PlotRange -> All,
PlotLabel -> "Decay Curve of Phosphorus-31 by \[Beta]^{+} Emission",
PlotTheme -> "Detailed", PlotStyle -> Red,
LabelStyle -> {Black, Bold}, {0.85, 0.75}],
Frame -> {{True, False}, {True, False},
FrameLabel -> {"Time /s",
"Counts Recorded in the Previous 15 seconds"},
ImageSize -> {700, 500}},
Inset[Framed[
Grid[{{"Run 0"}, {"N=" Length[
dataHist5] "/50"}, {"A=" seA}, {"t_{frac{1}{2}}=" PlusMinus[
halfLife,
sehalfLife]}, {"\[Chi]^{2}=" fit[
"ANOVATableSumsOfSquares"][[2]]}, {"Reduced \[Chi]^{2}=" fit[
"ANOVATableMeanSquares"][[2]]}}], Background -> White,


Data:

dataHist5 = {{Around[16.5, 1.5],
Around[77.8, 8.8]}, {Around[34.5, 1.5],
Around[60.5, 8.0]}, {Around[52.5, 1.5],
Around[63.8, 8.0]}, {Around[106.5, 1.5],
Around[42.4, 6.5]}, {Around[124.5, 1.5],
Around[41.7, 6.5]}, {Around[142.5, 1.5],
Around[14.6, 3.8]}, {Around[160.5, 1.5],
Around[33.9, 5.8]}, {Around[178.5, 1.5],
Around[29.4, 5.4]}, {Around[196.5, 1.5],
Around[33.5, 5.8]}, {Around[214.5, 1.5],
Around[30.9, 5.6]}, {Around[232.5, 1.5],
Around[31.1, 5.8]}, {Around[250.5, 1.5],
Around[21.5, 4.6]}, {Around[268.5, 1.5],
Around[4.3, 2.1]}, {Around[286.5, 1.5],
Around[6.4, 2.5]}, {Around[322.5, 1.5],
Around[7.5, 2.7]}, {Around[340.5, 1.5],
Around[4.5, 2.1]}, {Around[358.5, 1.5],
Around[11., 3.3]}, {Around[376.5, 1.5],
Around[14.0, 3.7]}, {Around[394.5, 1.5],
Around[14.0, 3.7]}, {Around[466.5, 1.5],
Around[0.6, 0.7]}, {Around[502.5, 1.5],
Around[2.2, 1.5]}, {Around[520.5, 1.5],
Around[9.4, 3.1]}, {Around[538.5, 1.5],
Around[4.1, 2.0]}, {Around[646.5, 1.5],
Around[2.2, 1.5]}, {Around[682.5, 1.5], Around[0.6, 0.7]}}


Background:

This is the results of a radioactive decay experiment. These are points obtained from gaussian models (which is why they aren't integer), then points are then plotted and a model applied (the model is y=Ae^-(kt)), and then weighted for the uncertainties (weighting is 1/uncertainty^2). The target is then to find A (this is the y-intercept), k (this is the decay constant and will give the half-life, t = ln(2) / k). Half-life being the main desired figured (the time is takes for the decay curve to have reduced by half). Then produces the reduced chi^2 as a measure of a goodness of fit. And express this on a singular graphic, much as m_goldberg has done below.

Having copied the code across - it errors. I have tried to implement what I thought were fixes but unsuccessfully. I changed NonLinearModelfitData to NonLinearModel[fitData,...], And on the very last line the Show is not closed due to a missing speech mark after 'emission', so I added that in. But it outputs this:

• What is the definition of uncertainties? Without it being defined, your fit doesn't work and no one can see what is wrong with the rest of your code. Mar 5 at 12:50
• uncertainties = dataHist5[[All, 2, 2]]; But in practical terms, it's the error bars around each of the data points in dataHist5 Mar 5 at 13:00

### Edit 2

Well, I have thought more about your code and managed to get what looks like a reasonable fit.

Here is my 2nd revision of your code. I don't have any knowledge of the problem domain, so I can't be sure this is a satisfactory answer, but it looks more like what I think you want.

dataHist5 =
{{Around[16.5, 1.5], Around[77.8, 8.8]},
{Around[34.5, 1.5], Around[60.5, 8.0]},
{Around[52.5, 1.5], Around[63.8, 8.0]},
{Around[106.5, 1.5], Around[42.4, 6.5]},
{Around[124.5, 1.5], Around[41.7, 6.5]},
{Around[142.5, 1.5], Around[14.6, 3.8]},
{Around[160.5, 1.5], Around[33.9, 5.8]},
{Around[178.5, 1.5], Around[29.4, 5.4]},
{Around[196.5, 1.5], Around[33.5, 5.8]},
{Around[214.5, 1.5], Around[30.9, 5.6]},
{Around[232.5, 1.5], Around[31.1, 5.8]},
{Around[250.5, 1.5], Around[21.5, 4.6]},
{Around[268.5, 1.5], Around[4.3, 2.1]},
{Around[286.5, 1.5], Around[6.4, 2.5]},
{Around[322.5, 1.5], Around[7.5, 2.7]},
{Around[340.5, 1.5], Around[4.5, 2.1]},
{Around[358.5, 1.5], Around[11., 3.3]},
{Around[376.5, 1.5], Around[14.0, 3.7]},
{Around[394.5, 1.5], Around[14.0, 3.7]},
{Around[466.5, 1.5], Around[0.6, 0.7]},
{Around[502.5, 1.5], Around[2.2, 1.5]},
{Around[520.5, 1.5], Around[9.4, 3.1]},
{Around[538.5, 1.5], Around[4.1, 2.0]},
{Around[646.5, 1.5], Around[2.2, 1.5]},
{Around[682.5, 1.5], Around[0.6, 0.7]}};
fitData = {#[[1, 1]], #[[2, 1]]} & /@ dataHist5;

Clear[A, k]
uncertainties = dataHist5[[All, 2, 2]];
fit =
NonlinearModelFit[fitData, A Exp[-k t], {A, k}, t, Weights -> 1/uncertainties^2]


{A, k} = {A, k} /. fit["BestFitParameters"];
{σA, σk} = fit["ParameterErrors"];
hLife = Log[2]/Around[k, σk];
seA = Around[A, σA];
halfLife = hLife[[1]];
sehalfLife = hLife[[2]];


Here is the combined plot:

Show[
Plot[fit[x], {x, 0, 800},
PlotRange -> All,
PlotTheme -> "Detailed",
PlotStyle -> Red,
Axes -> False,
Frame -> {{True, False}, {True, False}},
FrameLabel -> {"Time /s", "Counts Recorded in the Previous 15 seconds"},
ImageSize -> Large],
ListPlot[dataHist5, ImageSize -> Large],
Graphics[
Inset[
Framed[
Column[
{"Run 0",
Row[{"N=" , Length[dataHist5], "/50"}],
Row[{"A=" , seA}],
Row[{Subscript[t, 1/2], "=" , PlusMinus[halfLife, sehalfLife]}],
Row[{"χ^{2}=" , fit["ANOVATableSumsOfSquares"][[2]]}],
Row[{"Reduced χ^{2}=", fit["ANOVATableMeanSquares"][[2]]}]}],
Background -> White,
{Right, Top},
Scaled[{1.1, 1.2}]]],
PlotLabel ->
Row[{"Decay Curve of Phosphorus-31 by ", Superscript[β, "+"], " Emission"}]]


The above code is a very careful cut and paste from the latest version of my Mathematica notebook in which I worked on your problem. I ran the code in Mathematica V12.1.1 running on MacOS 10.13.4.

Please copy and paste the code into a new, empty notebook, run it in a new Mathematica session and see if it still gives you errors.

• Thank you, this has been a big help. I must've had so many errors in there by comparison. I wonder why my fitting for this set of data returns a different half life (Ln(2)/k) than when JimB did it in this question: mathematica.stackexchange.com/questions/239832/… - it's the same data and should be the same model. Thank you for helping fix the styling. Mar 9 at 14:35
• Hi. I tried to recreate this answer by copying the posted code, but it doesn't produce the attached graph, and I haven't managed to chase the error out. I believe there's 2, one I think is a square bracket missing in 'NonLinearModelfitData' to be 'NonLinearModel[fitData,...]. I'm not sure about the other, I think it's another bracket somewhere in the plot code Mar 9 at 15:25
• Added the background as mentioned and my attempt to recreate the graph Mar 9 at 17:01
• @Epideme There was an error in the Show code, a missing end quote in the PlotLabel option. I have fixed it. Mar 9 at 18:08
• Yeh I thought it was that, but it doesn't seem to be working. I think the NonLinearModelfitData needs a square bracket, but I've tried adding that in too. I've put the graph I get out in the question but i will have another go in the morning. thank you. Mar 10 at 0:01