0
$\begingroup$

Is there any way to quickly reduce the number of ticks without having to specify them all yourself? There are too many ticks in my plot to show them properly (in a small graph I use to inset into another graph). There is especially no room for accompanying numbers. Say I would like to keep only half the number of ticks (or only show the numbers for half of the ticks), is this possible without having to specify the exact position of all of them?

$\endgroup$
1
$\begingroup$

Edit:

   myticksX[min_, max_, n_, tickLen_] := 
  Module[{}, 
   ticks = Table[{i, i, {tickLen, 0}}, {i, 
      FindDivisions[{min, max}, n]}];
   ticks[[2 ;; -2 ;; 2]] = 
    ticks[[2 ;; -2 ;; 2]] /. {a_?NumericQ, 
       b_?NumericQ, {tickLen, 0}} :> {a, "", {tickLen, 0}};
   ticks];
myticksY[min_, max_, n_, tickLen_] := 
  Module[{}, 
   ticks = Table[{i, i, {0.01, 0}}, {i, FindDivisions[{min, max}, n]}];
   ticks[[2 ;; -2 ;; 2]] = 
    ticks[[2 ;; -2 ;; 2]] /. {a_?NumericQ, 
       b_?NumericQ, {tickLen, 0}} :> {a, "", {tickLen, 0}};
   ticks];
   Plot[Sin[x], {x, 0, 10}, Frame -> True, 
 FrameTicks -> {{myticksY[-1, 1, 4, 0.01], 
    None}, {myticksX[0, 10, 5, 0.01], None}}]

enter image description here

Original Answer: How about this?

myticksX[min_, max_, n_] := 
     Table[{i, i, {0.01, 0}}, {i, FindDivisions[{min, max}, n]}]
    myticksY[min_, max_, n_] := 
     Table[{i, i, {0.01, 0}}, {i, FindDivisions[{min, max}, n]}]
    Plot[Sin[x], {x, 0, 10}, Frame -> True, 
     FrameTicks -> {myticksX[0, 10, 5], myticksY[-1, 1, 5]}]

enter image description here

Or

Plot[Sin[x], {x, 0, 10}, Frame -> True, 
 FrameTicks -> {{myticksY[-1, 1, 3], None}, {myticksX[0, 10, 3], 
    None}}]

enter image description here

$\endgroup$
  • $\begingroup$ Great! Thanks (and without undocumented functions the danger of which you pointed out in your comment). Would it be simple to also add smaller unnumbered ticks in between (but keeping the spacing equal between all of them)? $\endgroup$ – Kvothe Apr 25 '19 at 14:53
1
$\begingroup$

You could use Charting`ScaledTicks instead of Automatic:

Plot[Sin[x], {x, 0, 2 Pi}, Ticks -> {
    Charting`ScaledTicks[{Identity,Identity}][##, {3, 3}]&, 
    Charting`ScaledTicks[{Identity,Identity}][##, {3, 3}]&
    }
]

enter image description here

or:

Plot[Sin[x], {x, 0, 2 Pi}, 
    Frame->True,
    FrameTicks -> {
        {
        Charting`ScaledTicks[{Identity,Identity}][##, {3,3}]&, 
        Charting`ScaledFrameTicks[{Identity,Identity}][##, {2, 2}]&
        },
        {
        Charting`ScaledTicks[{Identity,Identity}][##, {2, 2}]&, 
        Charting`ScaledFrameTicks[{Identity,Identity}][##, {2, 2}]&
        }
    }           
]

enter image description here

$\endgroup$
  • $\begingroup$ I am not getting your img with my version 12.0.0 for Microsoft Windows (64-bit) (April 6, 2019) $\endgroup$ – OkkesDulgerci Apr 25 '19 at 14:27
  • $\begingroup$ @Carl, thanks! Any idea how I can keep the spacing even between ticks though? (Compare the spacing with {3,3} to no Ticks option. The big spacing does not coincide with the small one anymore.) $\endgroup$ – Kvothe Apr 25 '19 at 14:33
  • $\begingroup$ @Okkes Does my update produce the same output in M11 and M12 for you (other than the decimal point)? $\endgroup$ – Carl Woll Apr 25 '19 at 14:42
  • $\begingroup$ @Kvothe Sorry, I don't understand what spacing you're talking about. $\endgroup$ – Carl Woll Apr 25 '19 at 14:42
  • $\begingroup$ @Carl, sorry I should have been more clear I guess. If you run this without a ticks option you will see that all ticks have equal spacing between them. If we reduce the amount of ticks using your option we see that the "bigger" spacing falls in between the smaller (unnumbered) ones so that the spacing is not equal between that and the others. I would prefer to take out ticks in such a way that this distance is also equal. I think that looks nicer. (of course that is asking a lot and I am already grateful for being able to reduce the number of ticks in a simple manner). $\endgroup$ – Kvothe Apr 25 '19 at 14:47

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.