[Tex/LaTex] pgfplots – how to specify ticks

pgfplots

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.

enter image description here

Best 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
}%