10
$\begingroup$

I would like to remove minor ticks from my plots.

For example, consider the following code:

Plot[Sin[x], {x, 0, 10}]

which generates:

enter image description here

Is there any away to get rid of minor ticks and get something like:

enter image description here

I'm looking for a simpler solution than directly assigning numbers like this:

    Plot[Sin[x], {x, 0, 10}, Ticks -> {{0, 2, 4, 6, 8, 10}, {-1, 1}}]

I found this solution. But this is also very complicated and seems outdated (it refers to very older version, 9, of mathematica).

Is there easier and updated solution?

$\endgroup$

3 Answers 3

8
$\begingroup$

With version 10 you can proceed using undocumented functionality as follows (tested with version 10.4):

linearTicks = {Charting`ScaledTicks[{Identity, Identity}][##, {20, 1}] &, 
   Charting`ScaledTicks[{Identity, Identity}][##, {2, 1}] &};

Plot[Sin[x], {x, 0, 10}, Ticks -> linearTicks]

plot

Or shorter with identical result:

linearTicks = {Charting`ScaledTicks[{Identity, Identity}][##, 20] &, 
   Charting`ScaledTicks[{Identity, Identity}][##, 2] &};

Plot[Sin[x], {x, 0, 10}, Ticks -> linearTicks]

plot

$\endgroup$
2
  • $\begingroup$ thanks so much! I am implementing 10-20 plots with different scales. So does it mean that I have to write out twenty different linearTicks specifications? Can you please let me know what {20,1} and {2,1} mean? Once again, thank you so much for your help! $\endgroup$
    – ppp
    Apr 10, 2016 at 18:31
  • $\begingroup$ @ppp The Charting`ScaledTicks function is undocumented but from my experiments the first digit means the maximum number of major ticks and the second number determines the number of intermediate minor ticks (between adjacent major ticks). And yes, in this way unfortunately you need to specify individual linearTicks for every plot AFAIK. It is a shame that Mathematica still has no built-n function without these shortcomings! $\endgroup$ Apr 10, 2016 at 18:39
8
$\begingroup$

You can use Range,

Plot[Sin[x], {x, 0, 10}, Ticks -> {Range[0, 10, 1]}]

enter image description here

Plot[Sin[x], {x, 0, 10}, Ticks -> {Range[0, 10, 2]}]

enter image description here

and with Frame

f[x_] := x^2 - 5 x + 7

g[x_] := Sin[2 x - \[Pi]/3]

Plot[{
f[x]
, g[x]}
, {x, -\[Pi], \[Pi]}
, FrameTicks -> {Range[-\[Pi], \[Pi], \[Pi]/4], Automatic}
, PlotTheme -> "Detailed"]

enter image description here

Edit

FrameTicks is verry helpfull

Plot[{
f[x]
, g[x]}
, {x, -\[Pi], \[Pi]}
, FrameTicks -> {Range[-\[Pi], \[Pi], \[Pi]/4], Range[0, 30, 5]}
, PlotTheme -> "Detailed"]

enter image description here

as well as Ticks

Plot[Sin[x], {x, 0, 10}, Ticks -> {Range[0, 10, 2], {-1, 1}}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ I like this solution. But minor ticks are still there on the vertical axis. Do you know how to remove them as well? $\endgroup$
    – ppp
    Apr 10, 2016 at 18:46
  • $\begingroup$ Is there any way to format {-1,1} for the vertical axis in the same way as that for the horizontal axis, i.e. {initial value, final value, incremental}? $\endgroup$
    – ppp
    Apr 10, 2016 at 19:56
6
$\begingroup$

With versions greater than 10.1

Plot[Sin[x], {x, 0, 10}, Ticks -> {Subdivide[10, 5], Subdivide[-1, 1, 1]}]

And

Plot[Sin[x], {x, 0, 4 \[Pi]}, Ticks -> {Subdivide[4 \[Pi], 6], Subdivide[-1., 1., 4]}]

Sin plot

$\endgroup$
1
  • $\begingroup$ Cool, I had not come across Subdivide yet. +1 for teaching me a new function! $\endgroup$
    – MarcoB
    Apr 10, 2016 at 21:48

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.