3
$\begingroup$

I have written the following function for creating tickmarks on the x and y axis. However, I don't understand that why it is not performing well at -0.1 and -0.6.

TickMark[Min_, Max_, Inc_] :=
  Table[
   If[
    Mod[IntegerPart[i*10], 10] === 0,
    {IntegerPart[i], IntegerPart[i], .02, Black},
    If[
     Mod[IntegerPart[i*10], 5] === 0,
     {i, i, .02, Black},
     {i, Null, .01, Black}
     ]
    ],
   {i, Floor[Min], Ceiling[Max], Inc}];

xAxis = TickMark[-2, 2, 0.1];

yAxis = TickMark[0, 4, 0.1];

Plot[x^2, {x, -1, 1}, Ticks -> {xAxis, yAxis}]

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ Try using Round instead of IntegerPart $\endgroup$
    – Carl Woll
    Jun 12, 2017 at 17:34
  • $\begingroup$ @CarlWoll: Does that resolve the issue? Can you elaborate please? $\endgroup$ Jun 12, 2017 at 17:35
  • 1
    $\begingroup$ Compare Round @ Table[10 i, {i, -2, 2, .1}] to IntegerPart @ Table[10 i, {i, -2, 2, .1}]. $\endgroup$
    – Carl Woll
    Jun 12, 2017 at 17:39
  • $\begingroup$ See question (7463) $\endgroup$
    – Carl Woll
    Jun 12, 2017 at 18:14

2 Answers 2

5
$\begingroup$

Update

Updated so that "1." prints as "1" ...

It looks like you are seeking a big labeled tick when the tick falls on 5 * Inc and a small non-labeled tick otherwise (I may have mis-read your intentions).

If that is the case (you can probably figure out how to edit if this is not the case) try:

TickMark2[Min_, Max_, Inc_] :=
 Table[
  If[Round[i, 10 Inc] - i == 0,
   {i, Round[i], .02},
   If[Round[i, 5 Inc] - i == 0,
    {i, i, .02},
    {i, "", .01}
    ]
   ],
  {i, Floor[Min], Ceiling[Max], Inc}
  ]

and then

xAxis = TickMark2[-2, 2, 0.1]
(* {{-2., -2, 0.02}, {-1.9, "", 0.01}, {-1.8, "", 0.01}, {-1.7,
   "", 0.01}, {-1.6, "", 0.01}, {-1.5, -1.5, 0.02}, {-1.4, "", 
  0.01}, {-1.3, "", 0.01}, {-1.2, "", 0.01}, {-1.1, "", 
  0.01}, {-1., -1, 0.02}, {-0.9, "", 0.01}, {-0.8, "", 0.01}, {-0.7, 
  "", 0.01}, {-0.6, "", 0.01}, {-0.5, -0.5, 0.02}, {-0.4, "", 
  0.01}, {-0.3, "", 0.01}, {-0.2, "", 0.01}, {-0.1, "", 0.01}, {0., 0,
   0.02}, {0.1, "", 0.01}, {0.2, "", 0.01}, {0.3, "", 0.01}, {0.4, "",
   0.01}, {0.5, 0.5, 0.02}, {0.6, "", 0.01}, {0.7, "", 0.01}, {0.8, 
  "", 0.01}, {0.9, "", 0.01}, {1., 1, 0.02}, {1.1, "", 0.01}, {1.2, 
  "", 0.01}, {1.3, "", 0.01}, {1.4, "", 0.01}, {1.5, 1.5, 0.02}, {1.6,
   "", 0.01}, {1.7, "", 0.01}, {1.8, "", 0.01}, {1.9, "", 0.01}, {2., 
  2, 0.02}} *)

and

yAxis = TickMark2[0, 4, 0.1];

Plot[x^2, {x, -1, 1}, Ticks -> {xAxis, yAxis}, TicksStyle -> Black]

Mathematica graphics

No harm in making the TicksStyle for each tick but since here they are all Black seems simpler to make a single statement to that effect.

$\endgroup$
2
  • $\begingroup$ (+1) Yes, you got me right but I don't want the integers to be printed like 1. and prefer them to be printed just 1. :) That is why my function is a little more complicated than yours. :) $\endgroup$ Jun 13, 2017 at 1:36
  • $\begingroup$ Updated so that "1." prints as "1" $\endgroup$ Jun 13, 2017 at 1:44
4
$\begingroup$

I would implement the a tick making function rather differently. I would

  • use the built-in function FindDivisions to get the divisions
  • use a simple helper function to convert the divisions into tick specifications.
  • remove the wired-in relationship between major and minor ticks and allow the user to specify separate increments for them.

Doing it that way avoids a lot of the problems your approach must deal with and produces a more capable function.

Here is a start of an implementation of such a function. For serious work it would need considerably more bullet proofing. It could also benefit from not have the color wired-in. Nevertheless, it offers all the capabilities of your function and a bit more.

tickSpec["Major", val_?NumberQ] := {val, val, .02, Black}
tickSpec["Minor", val_?NumberQ] := {val, Null, .01, Black}

tickMaker[min_, max_, minor_, major_] :=
   Module[{m, n, minorVals, majorVals , minorTicks, majorTicks},
     m = Round[(max - min)/major];
     majorVals = FindDivisions[{min, max, major}, m];
     n = Round[(max - min)/minor];
     minorVals = Complement[FindDivisions[{min, max, minor}, n], majorVals];
     majorTicks = tickSpec["Major", #] & /@ majorVals;
     minorTicks = tickSpec["Minor", #] & /@ minorVals;
     Sort[Join[minorTicks, majorTicks]]]

xAxis = tickMaker[-2, 2, .1, .5];
yAxis = tickMaker[0, 4, .1, .5];
Plot[x^2, {x, -1, 1}, Ticks -> {xAxis, yAxis}]

plot

Changing the relationship between the major and minor ticks is now simple. Here is an example with no minor ticks on the x-axis and minor ticks spaced by 1/4 on the y-axis.

xAxis = tickMaker[-2, 2, .5, .5];
yAxis = tickMaker[0, 4, .25, .5];
Plot[x^2, {x, -2, 2}, Ticks -> {xAxis, yAxis}]

plot

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