2
$\begingroup$

Let's say I have a listplot like so:

ListPlot[Prime[Range[25]]]

I want my plot to mark the x,y values for the values associated with say, the 5th, and 10th points in the input. I would be interested in any of the following:

  • marking simply the y values so have 11 and 29 in the y axis
  • marking both x and y axis: ie show {5,11} and {10,29} in the x and y axis respectively.

with, or without perpendicular lines connecting the points to axes.

The point of this, is to give the reader of the plot, a sense of scale for given points of interest.

Do I have to superimpose different plots on each other or can I do it by some config on the listplot itself?

$\endgroup$
  • $\begingroup$ Use Ticks ? For example: ListPlot[Prime[Range[25]], Ticks -> {{5, 10}, Prime[{5, 10}]}, Epilog -> {Gray, Dashed, Line[{{5, 0}, {5, Prime[5]}}], Line[{{10, 0}, {10, Prime[10]}}], Line[{{0, Prime[5]}, {5, Prime[5]}}], Line[{{0, Prime[10]}, {10, Prime[10]}}] }] $\endgroup$ – flinty Jul 15 at 22:28
  • $\begingroup$ ah! how did I miss that! yeah that would do the trick :) $\endgroup$ – Shb Jul 15 at 22:31
  • 2
    $\begingroup$ This is a bit more general: With[{points = {5, 10, 13}}, ListPlot[Prime[Range[25]], Ticks -> {points, Prime[points]}, Epilog -> {Gray, Dashed, {Line[{{#, 0}, {#, Prime[#]}}], Line[{{0, Prime[#]}, {#, Prime[#]}}]} & /@ points }]] $\endgroup$ – flinty Jul 15 at 22:31
  • $\begingroup$ @flinty little improvement on this ^ mathematica.stackexchange.com/a/226205/8988 $\endgroup$ – Shb Jul 19 at 18:22
0
$\begingroup$

This is a more readable and reusable version of the suggestion by @fliny

With[
  {
    points = Table[{n,Fibonacci[n]}, {n, 7}],
    sampleValues = {1,5,7}
  },
  ListPlot[
    points,
    Joined -> True,
    Ticks -> {sampleValues , points[[All,2]][[sampleValues]]},
    Epilog -> {
      Dashed,
      {
        Line[{Projection[points[[#]], {1,0}], points[[#]]}], (*X-axis projection*)
        Line[{Projection[points[[#]], {0,1}], points[[#]]}]  (*Y-axis projection*)
      } & /@ sampleValues
    }
  ]
]

it will produce the following:

enter image description here

| improve this answer | |
$\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.