3
$\begingroup$

I am trying to put specific ticks but in that process the minor ticks are not showing anymore. How to show the minor ticks as well. Here is an example:

ListLinePlot[Table[{x, x}, {x, 0, 10}]]

enter image description here

In the x-axis I want to put ticks 1,2,3,4...,10. So I do

ListLinePlot[Table[{x, x}, {x, 0, 10}],Ticks -> {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, All}]

enter image description here

But now I destroyed all the minor ticks on the x-axis. Is there any way to bring them back? Thanks.

$\endgroup$
2
  • 1
    $\begingroup$ Did any of the answers satisfied your need? There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. One weeks is enough wait. Participation is essential for the site, please do your part. $\endgroup$
    – rhermans
    Jul 11, 2018 at 18:24
  • $\begingroup$ thank you for reminder. I was actually hoping for some easy and elegant solution and thus was waiting for a bit. I thought this question is going to be duplicate because many must have experienced this issue. To me however both these solutions are perfectly good and both does the job. $\endgroup$
    – BabaYaga
    Jul 11, 2018 at 18:51

2 Answers 2

3
$\begingroup$

A function to add custom ticks to the ticks generated automatically (actually, to the ticks that are close to the automatically generated ones):

ClearAll[addTicks]
addTicks[axis_ : 1] :=  Module[{newticks = #, 
   autoticks =  Charting`FindTicks[{0, 1}, {0, 1}][## & @@ PlotRange[#2][[axis]]]},
     DeleteDuplicatesBy[First][Join[Transpose[{(1.) newticks, newticks}] , autoticks]]]&;

Examples:

llp = ListLinePlot[Table[{x, x^2}, {x, 0, 10}] , ImageSize -> 300];
 
Row[{llp, Show[llp, Ticks -> {addTicks[][Range[10], llp], 
     addTicks[2][Range[0, 100, 10], llp]}]}, Spacer[5]]

enter image description here

lp3d = ListPlot3D[Table[Sin[j^2 + i], {i, 0, 10}, {j, 0, 50, 10}], 
   DataRange -> {{0, 10}, {0, 50}} , ImageSize -> 300];

Row[{lp3d, Show[lp3d, Ticks -> {addTicks[][Range[0, 10, 2], lp3d], 
     addTicks[2][Range[0, 50, 10], lp3d]}] }, Spacer[5]] 

enter image description here

$\endgroup$
2
$\begingroup$

A bit of a kludge, but it works:

ListLinePlot[Table[{x, x}, {x, 0, 10}], 
    Ticks -> {Union[Table[{i, i, {.01, 0}}, {i, 10}], 
                    Table[{j/4, " ", {.005, 0}}, {j, 40}]], All}]

or

Ticks -> {Table[
   If[Mod[i, 4] == 0, {i/4, i/4, {.01, 0}}, 
                      {i/4, " ", {.005, 0}}], 
      {i, 40}], All}
$\endgroup$

Your Answer

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

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