# pgfplots - how to specify ticks?

I know how to specify ticks manually. But this is too much work if you have multiple plots. I am searching a way to do that automatically. What I want to specify is the number of ticks for an axis. Is there a way to just say how many ticks you want to have? I read about "try min ticks=3" and tried "try max ticks=3" but that would have been too easy...

EDIT: Figure

Here's the figure on what I like to have 5 ticks on the y-axis.

-
Ok this seems to be the answer. But can I do that axis independently? –  DaPhil Feb 1 '13 at 15:17
What do you mean with "axis independently"? You can just put \pgfplotsset{max space between ticks=50pt} in your preamble, then it will apply to all plots. –  Jake Feb 1 '13 at 15:18
I mean, I want to have 3 ticks on x-axis and 4 on y-axis. –  DaPhil Feb 1 '13 at 15:19
Ah, okay. You can't, unfortunately. Off the top of my head I can't really think of a situation where you would want this, though. The algorithm that determines the tick positions works pretty well, so applying the try min ticks and max space between ticks to both axes usually leads to good results. Could you provide an example of where you'd need to provide the values to the x and y axis independently? –  Jake Feb 1 '13 at 15:22
No I can't :) I am currently just deciding how to draw figures in the future and so I want to check out the possibilities! Can I ask how the algorithm works? What if 4 ticks are ok for the y-axis but not for the x-axis? I will provide an image on where I wanted 4 ticks on the y-axis. –  DaPhil Feb 1 '13 at 15:26
show 1 more comment

## 1 Answer

The tick placement algorithm works by determining the range of the axis, dividing that by the max space between ticks distance to get the desired number of ticks, and then chooses a step size of either 1, 0.5, 0.2, or 0.1 times the order of magnitude of the upper limit (so 0.1, 0.05, 0.02, or 0.01 if your largest value is something like 0.4; 100, 50, 20, or 10 if your largest value is something like 400).

So in effect, max space between ticks is used to specify how many ticks to use, but not all step sizes are allowed. This value can, at the moment, not be specified separately for the different axes (feature request?).

If you want to get step sizes of 0.25 so you can get five ticks in your example, you'll have to adjust an internal macro (or open a feature request). Putting the following chunk in your preamble will allow step sizes of 0.25 (or 2.5, or 25, etc.)

\makeatletter
\def\pgfplots@assign@default@tick@foraxis@normalizetickdist#1#2{%
\begingroup
\let\H=\pgf@xb
\expandafter\pgfmathfloat@decompose#2\relax\pgfmathfloat@a@S\H\pgfmathfloat@a@E
% modify the mantisse:
\ifdim\H<2pt
\ifdim\H<1.5pt
\H=1.0pt
\else
\H=2.0pt
\fi
\else
\ifdim\H<4.9999pt
\ifdim\H<2.25pt
\H=2.0pt\relax
\else
\ifdim\H<3.75pt
\H=2.5pt\relax
\else
\H=5.0pt\relax
\fi
\fi
\else
\ifdim\H<7.5pt
\H=5.0pt\relax
\else
\H=1.0pt\relax
\advance\pgfmathfloat@a@E by1
\fi
\fi
\fi
\pgfmathfloatcreate{\the\pgfmathfloat@a@S}{\pgf@sys@tonumber{\H}}{\the\pgfmathfloat@a@E}%
\pgfmath@smuggleone\pgfmathresult
\endgroup
}%

-
Thanks again. I maybe pull a request on that. –  DaPhil Feb 1 '13 at 16:41
add comment