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$

1 Answer 1

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$
4
  • $\begingroup$ Thank you It works but now I want to ask you what are the "___" for? $\endgroup$ Dec 31, 2017 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, 2017 at 3:34
  • $\begingroup$ Maybe also add a DeleteCases version? $\endgroup$ Dec 31, 2017 at 4:20
  • 1
    $\begingroup$ @MikeHoneychurch, great idea. Thank you. $\endgroup$
    – kglr
    Dec 31, 2017 at 4:26

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.