1
$\begingroup$

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?

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$ Oct 11, 2023 at 11:53
  • $\begingroup$ @AlexeiBoulbitch: I added an image showing the desired output. Thanks. $\endgroup$
    – Moo
    Oct 11, 2023 at 12:00

2 Answers 2

5
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ That works for me! Somehow I missed Filling -> None. Thanks. $\endgroup$
    – Moo
    Oct 11, 2023 at 12:37
6
$\begingroup$
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

$\endgroup$
2
  • $\begingroup$ I learn so much from your posts. Thank you +1. $\endgroup$
    – Moo
    Oct 11, 2023 at 15:48
  • 1
    $\begingroup$ @Moo, thank you for the upvote. $\endgroup$
    – kglr
    Oct 11, 2023 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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