1
$\begingroup$

I am making a framed plot. I want to remove some of the tick labels but display all the ticks. For example, I want {-1,0,1} as the the tick labels on the y axis but want to display all the ticks between -1 to 1.I have done the following and the plot is showing only the mentioned labels and not the ticks between them.

FrameTicks -> {{{-1, 0, 1}, Automatic}, {{-2,-1,0,1,2}, Automatic}}

Can someone help me with this?

$\endgroup$
  • 1
    $\begingroup$ Take a look at the documentation of Ticks (FrameTicks uses the same specification format) - you need to specify all the ticks you want, each with the appropriate label. If you don't want a label, you can use "" or Spacer[0] as label specification. $\endgroup$ – Lukas Lang Sep 20 at 7:46
1
$\begingroup$

You can use the internal function Charting`ScaledTicks

yticks = Charting`ScaledTicks[{Identity, Identity},
   "TicksLength" -> {.03, .02}][##, {3, 10}] &;

xticks = Charting`ScaledTicks[{Identity, Identity}, 
        "TicksLength" -> {.03, .02}][##, {6, 5}] &;

Plot[Sin[x], {x, -2.5, 2.5}, Frame -> True, Axes -> False, 
  FrameTicks -> {{yticks, Automatic}, {xticks, Automatic}}]

enter image description here

If the horizontal plot range were -Pi to Pi the function xticks would have major ticks at -3 and 3 in addition to your desired list of labeled ticks. You can remove the unwanted labels using ReplaceAll:

xticks = Charting`ScaledTicks[{Identity, Identity}, 
   "TicksLength" -> {.03, .02}][##, {6, 5}] /. {a : (3. | -3.), _, b___} :> {a, "", b}&;

Plot[Sin[x], {x, -Pi, Pi}, Frame -> True, Axes -> False, 
 FrameTicks -> {{yticks, Automatic}, {xticks, Automatic}}]

enter image description here

$\endgroup$
0
$\begingroup$

You may use the following workaround.

There is the possibility to put anything at at a specified tick. For that instead of figure specifying the tick you need to put a list: {figure, what-you-want}. For example, instead of your list {-1,0,1} you may put {{-1,"a"},{0,"b"},{1,"c"}} and you get a, b and c instead of -1, 0 and 1. More details you may find in Help.

Now to your specific task. Let us firsts make a list, lst1, with figures for ticks replaced by the empty strings:

lst1 = Table[{i, ""}, {i, -1, 1, 0.1}]

(*  {{-1., ""}, {-0.9, ""}, {-0.8, ""}, {-0.7, ""}, {-0.6, 
  ""}, {-0.5, ""}, {-0.4, ""}, {-0.3, ""}, {-0.2, ""}, {-0.1, 
  ""}, {0., ""}, {0.1, ""}, {0.2, ""}, {0.3, ""}, {0.4, ""}, {0.5, 
  ""}, {0.6, ""}, {0.7, ""}, {0.8, ""}, {0.9, ""}, {1., ""}} *)

Let us now replace the empty strings at -1, 0 and 1 to what you want to leave:

enter image description here

(* {{-1, "-1"}, {-0.9, ""}, {-0.8, ""}, {-0.7, ""}, {-0.6, 
  ""}, {-0.5, ""}, {-0.4, ""}, {-0.3, ""}, {-0.2, ""}, {-0.1, ""}, {0,
   "0"}, {0.1, ""}, {0.2, ""}, {0.3, ""}, {0.4, ""}, {0.5, ""}, {0.6, 
  ""}, {0.7, ""}, {0.8, ""}, {0.9, ""}, {1, "1"}}  *)

Now let us build a simple plot

Plot[Sin[x], {x, -2, 2}, Frame -> True, 
 FrameTicks -> {{lst2, Automatic}, {{-2, -1, 0, 1, 2}, Automatic}}]

enter image description here

Have fun!

$\endgroup$

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.