7
$\begingroup$

I want to increase the number of minor ticks in a plot, but I don't want to label them. I searched through this site and also Mathematica documentation, but I didn't find any solution yet. Could anyone please tell me how to do this.

For an example:

Plot[2*Sin[x], {x, 0, 10}, Frame -> True, 
 FrameTicks -> {True, True, False, False}, Axes -> True, 
 AxesOrigin -> {0, 0}]

I want to get 10 minor tick marks in between 0 and 1 and so on (in between major tick marks on both axis).

Here is the output plot

$\endgroup$

4 Answers 4

12
$\begingroup$

Here's another approach, very similar to swish's. The difference being that it should work on all sorts of plot ranges.

The idea is to define a ticks function with min_ and max_ (idea from Ticks in documentation):

ticks[min_, max_] := 
  Table[If[FractionalPart[i] == 0., {i, i, .06, Red}, {i, "", .02,Blue}], 
  {i, Floor[min], Ceiling[max], 0.1}]

Then the graph:

Plot[2*Sin[x], {x, -\[Pi], \[Pi]}, Frame -> True, 
  FrameTicks -> {ticks, ticks, False, False}, Axes -> True]

and we get:

enter image description here

we note that we could use any condition within the Table in ticks (e.g. use Switch or Which to get mid-ticks, etc.

Also, here is a version that let's the user specify ranges of noteworthy ticks directly:

r1 = Range[-3, 3, 0.2];

r2 = Range[-3, 3, 0.1];

tickfreq = 0.05;

ticks[min_, max_] := 
 Table[With[{val = Round[Abs@FractionalPart[i], 0.01]}, 
   Which[Chop[Min[Abs[r1 - val]]] == 0, {i, i, .06, Red}, 
    Chop[Min[Abs[r2 - val]]] == 0, {i, "", .04, Green}, 
    1 < 2, {i, "", .02, Blue}]], {i, Floor[min], Ceiling[max], 
   tickfreq}]

where tickfreq specifies the frequency of the blue (base-) ticks, r1 the red ticks (with labels), r2 the green additional ticks. Using then the PlotRange you specify in the comments, we get:

Plot[2*Sin[x], {x, -\[Pi], \[Pi]}, Frame -> True, 
 FrameTicks -> {ticks, ticks, False, False}, Axes -> True, 
 PlotRange -> {{-0.5, 3.5}, {-0.25, 0.3}}]

enter image description here

Alternatively, one could also use different ticks version for the axes, but I am sure you get the idea. I hope this helps.

$\endgroup$
14
  • $\begingroup$ Thanks a lot. I actually want to increase the height of minor ticks at middle of each major tick marks too.(minor ticks at -2.5,-1.5,-0.5,0.5,..)Could you please tell me how to do it? $\endgroup$
    – TMH
    Apr 30, 2013 at 18:05
  • $\begingroup$ If my plot range is PlotRange -> {{-0.5, 3.5}, {-0.25, 0.3}}Can I still use FractionalPart[i]? I just want to show integers in x axis, but I want to get y axis as -0.25,-0.15,-0.05,0.05,0.15,0.25. I tried to get this, but I still couldn't get it. Could you please tell me how to get it? $\endgroup$
    – TMH
    Apr 30, 2013 at 20:03
  • $\begingroup$ I tried with this, ticks[min_, max_] := Table[Switch[Abs@FractionalPart[i*10], 0.5, {i, i, .06, Red}, 0., {i, i, .04, Green}, _, {i, "", .02, Blue}], {i, Floor[min], Ceiling[max], 0.01}] Plot[2*Sin[x], {x, -\[Pi], \[Pi]}, Frame -> True, FrameTicks -> {Automatic, ticks, False, False}, PlotRange -> {{-0.5, 3.5}, {-0.25, 0.3}}, Axes -> True] But, I still couldn't get the expected output. $\endgroup$
    – TMH
    Apr 30, 2013 at 21:02
  • $\begingroup$ I am sorry for all the edits, the numerical values caused me some problems. I am using Chop now in a version that let's you specify r1 (red ticks, labeled) and r2 (green ticks), rest is blue ticks. I hope you are able to finally do your chart - good luck, I have to run! $\endgroup$ Apr 30, 2013 at 22:40
  • $\begingroup$ Thanks a lot Pinguin for paying attention on my problem. As I told my plot range is PlotRange -> {{-0.5, 3.5}, {-0.25, 0.3}} When I run the edited code, still I couldn't get the expected out come. I tried several times. Actually, I spend all the day for this, but still I couldn't successful. $\endgroup$
    – TMH
    May 1, 2013 at 0:50
4
$\begingroup$

You can always manually specify the ticks with its labels, size and style

ticks = ({#, "", {.01, .005}, Red} & /@ Range[0.1, .9, .1])~Join~Range[0, 10, 1]
Plot[2*Sin[x], {x, 0, 10}, Frame -> True, 
 FrameTicks -> {ticks, True, False, False}, Axes -> True, 
 AxesOrigin -> {0, 0}]
$\endgroup$
7
  • $\begingroup$ Thanks for the comment. Can I increase the height of ticks at 0.5,1,1.5,2,...and so on? $\endgroup$
    – TMH
    Apr 30, 2013 at 17:00
  • $\begingroup$ FindDivisions[] would be a very handy function here... $\endgroup$ Apr 30, 2013 at 17:05
  • $\begingroup$ @swish Thanks. I actually want sub ticks for all range and to increase the height of tick marks which are located at 0,0.5,1,1.5 and 2. (I don't want to increase the height of all ticks) How to edit the above code for that purpose? $\endgroup$
    – TMH
    Apr 30, 2013 at 17:16
  • $\begingroup$ @J.M. Thanks for the comment. But, I don't know how to use it for my problem. $\endgroup$
    – TMH
    Apr 30, 2013 at 17:17
  • 2
    $\begingroup$ @TMH You can make it yourself, just specify the list of ticks you want {tick1, tick2, ...} with tickN of format {position, label, {positive_size, negative_size}, style}. $\endgroup$
    – swish
    Apr 30, 2013 at 17:24
3
$\begingroup$

Here's a way that uses FindDivisions and the default ticks (via the undocumented ticks function Charting`ScaledTicks, which you can find all over this site):

ClearAll[myTicks, major, minor]; 
myTicks[div_, h_Symbol: Charting`ScaledTicks, 
  opts : OptionsPattern[Charting`ScaledTicks]] :=
 With[{styles = DeleteDuplicatesBy[#[[3]] &]@
      Charting`ScaledTicks[{Identity, Identity}, opts][0., 1.]},
   major[Charting`ScaledFrameTicks] := 
    Evaluate@ReplacePart[First[styles], {1 -> #, 2 -> ""}] &;
   major[Charting`ScaledTicks] := 
    Evaluate@ReplacePart[First[styles], {1 -> #, 2 -> #}] &;
   minor = Evaluate@ReplacePart[Last[styles], {1 -> #}] &;
   Flatten[
    MapThread[
     #1 /@ Flatten[#2] &,
     {{major[h], minor}, FindDivisions[{##}, div]}
     ],
    1]] &;

One can inspect Options[Charting`ScaledTicks], but "TicksLength" is the only one I can find a use for in this context. It can only be used to set the "positive" length (into the plot). The "negative" length is hard-coded to 0..

Example:

Plot[2*Sin[x], {x, 0, 10}, Frame -> True, 
 FrameTicks -> {
   {myTicks[{5, 11}, "TicksLength" -> {0.03, 0.015}], 
    myTicks[{5, 11}, Charting`ScaledFrameTicks, "TicksLength" -> {0.03, 0.015}]},
   {myTicks[{11, 11}], 
    myTicks[{11, 11}, Charting`ScaledFrameTicks]}},
 Axes -> True, AxesOrigin -> {0, 0}]

Mathematica graphics


Folks who like to hijack the system may be amused by this:

ClearAll[myTicks];
myTicks[div_, h_: Charting`ScaledTicks] := 
  Internal`InheritedBlock[{Visualization`Divisions},
    Unprotect[Visualization`Divisions];
    Visualization`Divisions[{##}, {6, 6}, 10, ScalingFunctions -> {Identity, Identity}] := 
     FindDivisions[{##}, div];
    Protect[Visualization`Divisions];
    h[{Identity, Identity}][##]
    ] &;

It works similar to the above, but without the options. It basically allows you to set the tick divisions from the hard-coded default {6, 6} to whatever you like.

$\endgroup$
1
$\begingroup$

We can (1) use the optional third argument of Charting`ScaledTicks[..] to specify the number of major and minor ticks (2) use the same ticks spec for left and right (and bottom and top) frames, (3) hide the labels in top and right frames using FontOpacity -> 0 as the FrameStyle:

Plot[2*Sin[x], {x, 0, 10}, Frame -> True,
  FrameStyle -> {{Automatic, FontOpacity -> 0}, {Automatic, FontOpacity -> 0}}, 
  FrameTicks -> {{#, #} & @ (Charting`ScaledTicks[{Identity, Identity}, 
      "TicksLength" -> {0.03, 0.015}][##, {5,  11}] &), 
    {#, #} & @ (Charting`ScaledTicks[{Identity, Identity}, 
      "TicksLength" -> {0.03, 0.015}][##, {11, 11}] &)}, 
  Axes -> True, AxesOrigin -> {0, 0}]

enter image description here

$\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.