2
$\begingroup$

I would like to have Ticks->Automatic, but not have the Ticks in say the first 5% of the range on the x-axis. So like this

but not this (ticks too dense using regular tick interval of 1) enter image description here

$\endgroup$
2
$\begingroup$

It is probably easiest to create the plot with automatic ticks and then remove the ones you don't want. Something like

trimTicks[g_Graphics, a_] := Module[{opt, xmin, newticks},
  opt = AbsoluteOptions[g, {PlotRange, Ticks}];
  xmin = First[PlotRange /. opt].{1 - a, a};
  newticks = Cases[First[Ticks /. opt], {x_ /; x >= xmin, __}];
  Show[g, Ticks -> {newticks, Automatic}]]

Plot[x, {x, 0, 100}] ~trimTicks~ 0.2

enter image description here

$\endgroup$
  • $\begingroup$ Is there a way to create an Automatic Ticks distribution starting from the (rounded/Ceiling'd) minimum value, like the x coordinate fo "12" in the top figure? Would make it even better! $\endgroup$ – user18798 Feb 20 '16 at 10:07
  • $\begingroup$ I don't think so. Ticks are either fully automatic or explicitly specified. $\endgroup$ – Simon Woods Feb 20 '16 at 10:24
  • 1
    $\begingroup$ @SimonWoods Actually Ticks also can be Dynamically generated by a user-defined function, please see my answer. $\endgroup$ – Alexey Popkov Feb 20 '16 at 15:12
  • $\begingroup$ @AlexeyPopkov, good point. I had forgotten that you could supply a function for Ticks. $\endgroup$ – Simon Woods Feb 20 '16 at 16:12
4
$\begingroup$

Probably you are looking for something like this (code for version 10):

myTicksRange[min_, max_] := 
  Sequence[Ceiling[min + 0.05 (max - min)], Floor[max - 0.05 (max - min)]];

linearFrameTicks = {
   {Most /@ Charting`ScaledTicks[{Identity, Identity}][myTicksRange[##]] &, 
    Most /@ Charting`ScaledFrameTicks[{Identity, Identity}][myTicksRange[##]] &}, 
   {Most /@ Charting`ScaledTicks[{Identity, Identity}][myTicksRange[##], {12, 5}] &, 
    Most /@ Charting`ScaledFrameTicks[{Identity, Identity}][myTicksRange[##], {12, 5}] &}
                    };

Plot[3 Sin[x], {x, 0, 13}, Frame -> True, FrameTicks -> linearFrameTicks]

plot

Note that with this approach you have Dynamically generated Ticks what requires myTicksRange to be defined when FrontEnd renders the figure.

In this answer of mine you can find additional information and also code for log-plots.

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