5
$\begingroup$

How can I delete tick labels -2 and 2 so that the values -1.8 and 1.8 doesn't overlap with each other; i.e., I want to keep the ticks at -2 and 2, but not their labels.

This is the code I am using:

plot = Plot[{-6 x - x^3/3 + 10 ArcTan[x]}, {x, -2.2, 2.2}]
ticks = Charting`FindTicks[{0, 1}, {0, 1}] @@ PlotRange[plot][[1]];
ticks2 = Charting`FindTicks[{0, 1}, {0, 1}] @@ PlotRange[plot][[2]];
newticks = {{0.72, 0.72}, {-0.72, -0.72}}~Join~ticks;
newticks2 = {{1.80, 1.80}, {-1.80, -1.80}}~Join~ticks2;
Show[plot, Ticks -> {newticks, newticks2}]

Overlaped plot

$\endgroup$
7
$\begingroup$

Modify newticks2 to remove the unwanted ticks:

newticks3 = newticks2 /. {2. | -2., __} :> Sequence[]; (* or *)
newticks3 = DeleteCases[newticks2, {2. | -2., __}]  (* thanks: MikeHoneychurch *)

Show[plot, Ticks -> {newticks, newticks3}]

enter image description here

Update: To remove the labels and modify the major ticks to minor ticks at 2 and -2:

newticks4 = newticks2 /. {x : 2. | -2., __} :> {x, "", {0.005, 0.}, {Thickness[0.001]}};  
Show[plot, Ticks -> {newticks, newticks4}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you It works but now I want to ask you what are the "___" for? $\endgroup$ – Shai Lèger Dec 31 '17 at 3:25
  • $\begingroup$ @Shai, you are welcome. ___ (BlankNullSequence) is a pattern object that can stand for any sequence of zero or more Mathematica expressions. Btw, I meant to use __ (BlankSequence) . Updated with the correction. $\endgroup$ – kglr Dec 31 '17 at 3:34
  • $\begingroup$ Maybe also add a DeleteCases version? $\endgroup$ – Mike Honeychurch Dec 31 '17 at 4:20
  • 1
    $\begingroup$ @MikeHoneychurch, great idea. Thank you. $\endgroup$ – kglr Dec 31 '17 at 4:26

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.