7

I am a rookie in the use of Mathematica. How do I place a label next to the point where two lines intersect?

q[x_] := Q - (7 Q x)/(4 L) + (3 Q x^2)/(4 L^2) /. {Q -> 1, L -> 1}
line1 = Line[{{0, Q/2}, {L/3, Q/2}}] /. {Q -> 1, L -> 1};
line2 = Line[{{L/3, 0}, {L/3, Q/2}}] /. {Q -> 1, L -> 1};
Show[
  Plot[q[x], {x, 0, 1}, 
    Epilog -> {Directive[{Thick, Red, Dashed}], line1, line2}], 
    AxesLabel -> {HoldForm[Posição na Barra], HoldForm[Carga]}, 
    PlotLabel -> HoldForm[Distribuição da Carga na Barra], 
    LabelStyle -> {GrayLevel[0]}]

enter image description here

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
LCarvalho
  • 9,233
  • 4
  • 40
  • 96
  • Add an Inset or Text containing the label in the Epilog for your plot. Specify the inset position using the coordinates of the intersection point, which you already know. – MarcoB Jun 04 '16 at 02:46
  • @MarcoB Thank you for your help – LCarvalho Jun 04 '16 at 04:20

2 Answers2

7

Since you are, as you say, a rookie, you might be interested in how much your code can simplified.

With[{Q = 1, L = 1},
  q[x_] := Q - (7 Q x)/(4 L) + (3 Q x^2)/(4 L^2);
  line1 = Line[{{0, Q/2}, {L/3, Q/2}}]; 
  line2 = Line[{{L/3, 0}, {L/3, Q/2}}];
  pt = {L/3, Q/2}];

Plot[q[x], {x, 0, 1}, Epilog -> {Directive[{Thick, Red, Dashed}], line1, line2, Black, PointSize[Large], Point[pt], Text[pt, Offset[{20, 10}, pt]]}, AxesLabel -> {"Posição na Barra", "Carga"}, PlotLabel -> "Distribuição da Carga na Barra", LabelStyle -> Black]

plot

Notes

  1. Using With rather than multiple replacements has the advantage of being easier to write and less error prone (each additional replacement is another chance to mis-type a value). It is also faster in execution.

  2. Unless you are using a version of Mathematica older than V6, you don't need Show.

  3. HoldForm can be useful in building labels, but in your case simple text strings will work.

  4. The label text can be placed using Offset, which works in printers points and is thus independent of the plot coordinate system.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • 1
    a much more useful answer than mine. Offset is nice. +1. :) – ubpdqn Jun 04 '16 at 12:57
  • @ubpdqn I hope to learn a lot on this site. I'm realizing that there are many professionals here – LCarvalho Jun 04 '16 at 22:37
  • @m_goldberg In the Wolfram Documentation, the command text describes the following: Text[expr,coords] Because it was necessary to insert the command in this way?: Text[pt, offset[{20, 10}, pt]]} As "@ubpdqn" managed by inserting this way: Text[Framed["Label", Background -> White], {1/3, q[1/3]}, #]} – LCarvalho Jun 05 '16 at 05:37
  • @m_goldberg I am referring to the use of the term offset – LCarvalho Jun 05 '16 at 05:46
  • @LeandroMacieldeCarvalho. I'm not sure what your comments are asking. There are many ways to specify the position (2nd arg) in Text, My using Offset is not necessary but rather a matter of convenience that simplifies the code in this particular case. I didn't want the label to fall right over the point, but to be shifted to the right and upward so that it stands clear of the point. – m_goldberg Jun 05 '16 at 05:58
  • @LeandroMacieldeCarvalho sorry I missed this discussion. As I commented to m_golberg, his answer is much more instructive and I voted for it and am glad you accepted. The notes are very instructive. Good luck :) – ubpdqn Jun 05 '16 at 06:32
  • @m_goldberg You perfectly understood my question. Only had not understood as "@ubpdqn" managed to position the text only with #. Forgive me also if I misunderstood because I'm Brazilian and not usually very write in English. – LCarvalho Jun 05 '16 at 06:49
5

You can use Text in the Epilog of 2D plots. For example (using the given functions and lines. I have removed Show as I was unsure why it was required):

plots = Plot[q[x], {x, 0, 1}, 
     Epilog -> {Directive[{Thick, Red, Dashed}], line1, line2,
       Green, PointSize[0.02], Point[{1/3, q[1/3]}], Black, 
       Dashing[0], 
       Text[Framed["Label", Background -> White], {1/3, q[1/3]}, #]}, 
     AxesLabel -> {HoldForm[Posição na Barra], HoldForm[Carga]}, 
     PlotLabel -> 
      Column[{HoldForm[Distribuição da Carga na Barra], 
        Row[{"offset: ", #}]}], LabelStyle -> {GrayLevel[0]}, 
     ImageSize -> 300] & /@ {Automatic, {0, 0}, {0, 1}, {1, 0}, {1, 
     1}, {0, -1}, {-1, 0}, {-1, -1}};
Grid[Partition[plots, 2]]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • As I am beginner and receive answers that contains more information than I expected, it's fantastic. Thank you very much. I really want to learn this program ... – LCarvalho Jun 04 '16 at 12:06
  • @LeandroMacieldeCarvalho you are welcome. There are many ways to do things in Mathematica. So play and if you get stuck ask a question here. You posted your code and explained what you wanted. I just showed you a few things. The best way to learn is play. If you get stuck then there are diverse creative users here. I have and continue to learn from them. Have fun! :) – ubpdqn Jun 04 '16 at 12:11
  • You can also use Inset instead of Text if you want the label to be a more complicated graphical object, e.g., two lines of text using Inset[Column[{"line one","line two"}],{x,y}]. – Jess Riedel Jun 04 '16 at 12:49
  • I noticed that the term # is used both to offer the coordinates of the text in the same way as the string that is used in column. – LCarvalho Jun 05 '16 at 05:17