5
$\begingroup$

I have the following example data

os = {1, 3, 9, 11, 13, 17}; 
data = {0, -9.000050000000037`, -18.000100000000074`,
   -27.00015000000011`, -36.00020000000015`, -16.00020000000022`, 
   3.999799999999709`, 23.999799999999638`, 43.99979999999957`, 
   46.49974999999955`, 48.999699999999535`, 54.999749999999636`, 
   60.99979999999974`, 66.49984999999975`, 71.99989999999977`, 
   79.99989999999977`, 87.99989999999977`, 81.99939999999938`, 
   75.998899999999`, 69.99839999999861`, 63.997899999998225`, 
   79.4977499999973`, 94.99759999999637`, 107.49759999999637`, 
   119.99759999999637`, 138.99784999999693`, 157.9980999999975`, 
   153.49824999999836`, 148.99839999999924`, 155.49789999999885`, 
   161.99739999999846`, 175.49764999999834`, 188.99789999999822`};

And I make the following plot

ListLinePlot[data, PlotRange -> All, PlotStyle -> Black, 
GridLines -> {(os - 1) 2 + 1, data[[(os - 1) 2 + 1]]}, 
GridLinesStyle -> Directive[Black]]

enter image description here

I would like the y-axis to show the automatic ticks as right now, as well as ticks at the grid line locations.

I tried adding the property Ticks -> {Automatic, {Automatic, data[[(os - 1) 2 + 1]]}}, but this gives a syntax error. What is the correct syntax to do that?

$\endgroup$

1 Answer 1

8
$\begingroup$

You can use AxisObject to add additional ticks. For example, for the ticks on the horizontal axis:

ListLinePlot[
    data, 
    PlotRange -> All, 
    PlotStyle -> Black, 
    GridLines -> {(os - 1) 2 + 1, data[[(os - 1) 2 + 1]]}, 
    GridLinesStyle -> Directive[Black],
    Epilog -> AxisObject[
        Line[{{0,0},{34,0}}], 
        {0,34}, 
        AxisStyle->Opacity[0],
        TickPositions->{{(os - 1) 2 + 1}}
    ]
]

enter image description here

For the vertical ticks, things are trickier, because ListLinePlot automatically uses PlotRangeClipping, which will clip the labels. The following will work, but won't clip any graphics elements that need to be clipped:

ListLinePlot[
    data, 
    PlotRange -> All, 
    PlotStyle -> Black, 
    GridLines -> {(os - 1) 2 + 1, data[[(os - 1) 2 + 1]]}, 
    GridLinesStyle -> Directive[Black],
    AxesOrigin -> {0, 0},
    ImagePadding->{{50,0},{10,10}},
    Epilog -> AxisObject[
        Line[{{0, -50}, {0, 200}}],
        {-50, 200},
        AxisStyle->Opacity[0],
        TickPositions->{{Round @ data[[(os-1) 2 + 1]]}}
    ],
    PlotRangeClipping->False
]

enter image description here

I used Round so that the labels aren't clipped by ImagePadding. If you want to avoid Round, you will need to increase the image padding on the left. I also explicitly set the axis origins.

$\endgroup$
2
  • 3
    $\begingroup$ Thank you! I found that in this particular case AxesOrigin -> {0, 0}, Epilog -> { AxisObject[{"Horizontal", 0}, TickPositions -> {{(os - 1) 2 + 1}}], AxisObject[{"Vertical", 0}, TickPositions -> {{Round@data[[(os - 1) 2 + 1]]}}]} is sufficient to have all labels appear. $\endgroup$
    – Kagaratsch
    Jan 20 at 23:32
  • $\begingroup$ Looks like the PlotRangeClipping issue is a bug fixed in M13. $\endgroup$
    – Carl Woll
    Jan 21 at 1:26

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.