1
$\begingroup$

I have a simple code to generate a plot

 ListPlot[Table[4.5 (x - 5) + RandomReal[{-20, 20}], {x, 5, 100, 10}]]

I would like each point to have two red lines intersecting the x and y axes along with a label that gives the ratio=y/x for each point.

$\endgroup$
3
$\begingroup$

Like this?

tbl = Table[{x, 4.5 (x - 5) + RandomReal[{-20, 20}]}, {x, 5, 100, 10}];
Show[ListPlot[tbl], 
  Graphics[{Red, 
    Map[{x = #[[1]]; y = #[[2]];
      Line[{{0, y}, {x, y}, {x, 0}}], 
      Text[ToString[y/x], {x, y}, {0, -1}]} &, tbl]}]]

enter image description here

$\endgroup$
  • $\begingroup$ Yes! Thank you! $\endgroup$ – Giovanni Baez Aug 21 '18 at 20:50
  • 1
    $\begingroup$ There is a lot going on in that. Study the details. Look up each of those functions in the help system. Try little changes. See how much you can learn from that so you will be able to use some of those ideas for your next problem. You are trying to learn how to think in a way that you will likely guess how to write things that look like this and work like this $\endgroup$ – Bill Aug 21 '18 at 20:52
1
$\begingroup$
xs = Range[5, 100, 10]
idxs = Range@Length@xs
ys = Table[4.5 (x - 5) + RandomReal[{-20, 20}], {x, xs}]
labels = ys/xs
vlines = InfiniteLine[{{#1, 0}, {#1, 1}}] & /@ idxs
hlines = InfiniteLine[{{0, #}, {1, #}}] & /@ ys
ListPlot[ys -> labels,
 Prolog -> {LightRed, vlines, hlines}]

enter image description here

It probably looks better framed.

enter image description here

$\endgroup$
1
$\begingroup$

Or like the following:

pnts = Table[{x, 4.5 (x - 5) + RandomReal[{-20, 20}]}, {x, 5, 100, 10}];
lines = Line[{{0, Last[#]}, #, {First[#], 0}}] & /@ pnts;
slopes = Text["m = dy/dx = " <> ToString@N[Divide@@Reverse[#]], #*{0.5, 1}, {0, -1}] & /@ pnts;

Show[Graphics[{PointSize[0.02], Point/@pnts, Red, lines, Blue, slopes}],
  Axes -> True, PlotRange -> {{0, 100}, {0, 500}}, AspectRatio -> 1 
 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.