# Add lines and text in a plot

If I have the following code:

data = {{2, 66.9795853828354}, {Log[300]/Log[10],
70.18157017698786}, {Log[600]/Log[10], 71.76921354704794}, {3,
72.4945695680508}, {-2.3010299956639813,
54.00231502199229}, {-(Log[60]/Log[10]),
55.1941444178587}, {-(Log[20]/Log[10]), 56.003834022541035}, {-1,
56.949679030549746}, {-(Log[6]/Log[10]),
57.3050045190135}, {-(Log[10/3]/Log[10]),
57.72132372329668}, {-(Log[2]/Log[10]),
58.24888194249831}, {-2.3010299956639813,
54.03670125564749}, {-(Log[60]/Log[10]),
55.11571197888246}, {-(Log[20]/Log[10]), 56.17041542461532}, {-1,
56.71167863055032}, {-(Log[6]/Log[10]),
57.25059302294601}, {-(Log[10/3]/Log[10]),
57.70966183063876}, {-(Log[2]/Log[10]), 58.106793148660984}};

eqn = ((log10q - Log10[qref]) ==
c1*(Tfp - Tfpref)/(c2 + (Tfp - Tfpref)));
model = Tfp /. Solve[eqn, Tfp][[1]] // FullSimplify;
const = {Tfpref -> 57.25, qref -> 10/60 };
model2 = model /. (const // Rationalize) // FullSimplify;

nlm = NonlinearModelFit[data, {model2, c1 > 5, c2 > 5}, {c1, c2},
log10q];


Which plotted with the following code gives:

Show[{ListPlot[data, PlotStyle -> {Red, AbsolutePointSize[6]},Frame -> True, Axes -> False, FrameStyle -> 16, ImageSize -> Large,GridLines -> Automatic, GridLinesStyle -> Lighter[Gray, .8],FrameLabel -> (Style[#, 20, Bold] & /@ {HoldForm@Log10["q (K/s)"], Row[{Subscript[T, f'], " (", Degree, " C)"}]})], Plot[nlm[log10q], {log10q, Min[data[[All, 1]]], Max[data[[All, 1]]]}]},(*The legends in the Epilog start here*)Epilog -> Inset[Framed[Column[{PointLegend[{Red}, {"Data"}, {LegendMarkerSize -> 15}, {LabelStyle -> 15}],LineLegend[{Blue}, {"Fit"}, {LegendMarkerSize -> 15}, {LabelStyle -> 15}]}], RoundingRadius -> 10],Scaled[{0.35, 0.8}]]]


Question:

How can I add text in the Figure as to create a plot similar to this (done with Paint)?:

Where in the image I have two texts "Range 1" and "Range 2" as well as two red lines separating the two ranges.

• Look up Epilog and Prolog, Inset, Text, Style. In short: Plot[..., Epilog -> {Inset[Style["Range1", Bold, 14], <positioncontrols>], Red, Line[{firstpoint, last point}], ...}]. I am pretty sure this has come up before on the site. Have you made it a habit to do a search of this site before posting? You could get your answers quicker that way. Jun 3 '20 at 22:09
• You could also combine a Graphics object containing Inset and Lines with your plots using Show. Jun 3 '20 at 22:11
• @MarcoB. Two things: 1)Yes, I looked up but I couldn't see something with text and lines together.2) I am already using Epilog for the Legends and I am not sure how to implement a second Epilog for the text and lines. Could you give me an example if you don't mind on how to do this implementation or doing it with Graphics or Show?
– John
Jun 3 '20 at 22:12
• Just a few of the top results from a quick search here ("add text / line plot"): Add text or descriptions to Plot; How to add a vertical line to a plot; Add region-labels below x-axis. Jun 3 '20 at 22:16
• @MarcoB this is very helpful and I did not find this one before. I think I should be able to solve this problem with this. Should I erase this post?
– John
Jun 3 '20 at 22:32

texts = MapThread[Text[Style["Range "<>#, 20, FontFamily -> Times], #2, {0, 0}, {0, 1}]&,
{{"1", "2", "3"}, {{1, 57}, {2, 58}, {2.75, 59}}}];

epilog = {texts,
First @ Plot[nlm[log10q], {log10q, Min[data[[All, 1]]], Max[data[[All, 1]]]}]};

legend = Placed[
LineLegend[{Red, ColorData[97][1]}, {"Data", "Fit"},
Joined -> {False, True},
LegendMarkers -> {Automatic, None},
LegendFunction -> "Frame"],
Scaled[{.2, .8}]];

data2 = {{Log10[30], 64.5}, {Log10[300], 69.5}};

ListPlot[{data, data2},
PlotStyle -> {Directive[Red, AbsolutePointSize[6]], None},
Frame -> True, Axes -> False, FrameStyle -> 16, ImageSize -> Large,
GridLines -> Automatic, GridLinesStyle -> Lighter[Gray, .8],
FrameLabel -> (Style[#, 20, Bold] & /@ {HoldForm@Log10["q (K/s)"],
Row[{Subscript[T, f'], " (", Degree, " C)"}]}),
Filling -> {2 -> 50},
FillingStyle -> Directive[Opacity[1], Red, Dashed, Thickness[0.008]],
Epilog -> epilog,
PlotLegends -> legend]


• kglr thank you!! This is also a very nice way to solve the problem! I appreciate it !
– John
Jun 4 '20 at 1:05

Thanks to the suggestions of @MarcoB I was able to do it. Here's one way to do it:

  Show[ListPlot[data, PlotStyle -> {Red, AbsolutePointSize[6]},
Frame -> True, Axes -> False, FrameStyle -> 16, ImageSize -> Large,
GridLines -> Automatic, GridLinesStyle -> Lighter[Gray, .8],
FrameLabel -> (Style[#, 20, Bold] & /@ {HoldForm@Log10["q (K/s)"],
Row[{Subscript[T, f'], " (", Degree, " C)"}]})],
Plot[nlm[log10q], {log10q, Min[data[[All, 1]]],
Max[data[[All, 1]]]}],(*The legends in the Epilog start here*)
Epilog -> {Inset[
Framed[Column[{PointLegend[{Red}, {"Data"}, {LegendMarkerSize ->
15}, {LabelStyle -> 15}],
LineLegend[{Blue}, {"Fit"}, {LegendMarkerSize ->
15}, {LabelStyle -> 15}]}], RoundingRadius -> 10],
Scaled[{0.35, 0.8}]],
Rotate[Text[Style["Range 1", 20, FontFamily -> Times], {1, 57}],
90 Degree],
Style[Line[{{Log10[30], 50}, {Log10[30], 64.5}}], Red, Dashed,
Thickness[0.008]],
Rotate[Text[Style["Range 2", 20, FontFamily -> Times], {2, 58}],
90 Degree],
Style[Line[{{Log10[300], 50}, {Log10[300], 69.5}}], Red, Dashed,
Thickness[0.008]],
Rotate[Text[Style["Range 3", 20, FontFamily -> Times], {2.75, 59}],
90 Degree]}]
`

Which gives:

• That’s very nice! Glad you were able to piece it together! (+1) Jun 4 '20 at 2:53