1

I wanted a discrete plot of $y = -\dfrac{3}{2} x$ and used

  DiscretePlot[-3/2 x, {x, -20, 20}]

I do not like the vertical lines shown in the plot but I did not see an option to get rid of them.

I then did this kludge to get rid of the vertical lines

  ListPlot[Table[{x, -((3 x)/2)}, {x, Table[x, {x, -20, 20, 1}]}]]

I want to add arrowheads showing that the plot continues upward on the left and downward on the right.

I found this answer that uses BaseStyle, but could not figure out how to use it to place arrows at each endpoint showing direction.

  ListPlot[Table[{x, -((3 x)/2)}, {x, Table[x, {x, -20, 20, 1}]}], 
   BaseStyle -> Arrowheads[{-20, 30, 20, -30}], 
   PlotStyle -> {Blue, Thick}] /. Line -> Arrow

I wanted something like this

enter image description here

Is there a simple way to do this?

Moo
  • 3,260
  • 1
  • 12
  • 28
  • 1
    The option Filling->None removes lines. From your text, it is not clear where you want to have the arrowheads. If it is at the axis add this option: AxesStyle->Directive[Arrowheads[0.005]]. If you have something else in mind, please sketch what you are after. – Alexei Boulbitch Oct 11 '23 at 11:53
  • @AlexeiBoulbitch: I added an image showing the desired output. Thanks. – Moo Oct 11 '23 at 12:00

2 Answers2

6
addArrowTips = ReplaceAll[Point[x_] :> 
    {Point[Take[x, {2, -2}]], 
     Map[Arrow] @ Extract[x, {{{2, 1}}, {{-2, -1}}}]}];

addArrowTips replaces the points at the two ends of a point list with short arrows. We can use it as the option setting for DisplayFunction .

Alternatively, we use it as addArrowTips @ DiscretePlot[...] to post-process DiscretePlot output.

Examples:

DiscretePlot[-3/2 x, 
 {x, -20, 20}, 
 Filling -> None, 
 DisplayFunction -> addArrowTips]

enter image description here

DiscretePlot[{-3/2 x, x^3/100, x^2/5 + 20}, 
 {x, -20, 20}, 
 Filling -> None, 
 DisplayFunction -> addArrowTips]

enter image description here

DiscretePlot[{x Sin[2 x], 5 + x Cos[2 x]}, 
 {x, -6, 6, Pi/40}, 
 Filling -> None, 
 PlotStyle -> PointSize[Medium], 
 DisplayFunction -> addArrowTips]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
5

One possible way could be:

Clear["Global`*"];
c1 = {#, -3/2 #} & /@ Range[20, 21] // N;
c2 = {#, -3/2 #} & /@ Range[-20, -21, -1] // N;

DiscretePlot[-3/2 x, {x, -20, 20} , Filling -> None , Epilog -> { Arrowheads[0.05], Darker@Cyan, Arrow[c1], Arrow[c2] } ]

enter image description here

Syed
  • 52,495
  • 4
  • 30
  • 85