9

Firstly I would like a minor tick between every major tick in my pgfplots graph (and minor grids). I wrote minor tick num=1 but it doesn't work. I am not using a logarithmic axis so it is unrelated to other questions.

Secondly, why is the tick label "1" missing for each axis? How do I get that to display?

No minor ticks

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
footnotesize,
width=4in,
height=4in,
axis equal,
scale only axis,
separate axis lines,
grid=both,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
xmin=0,
xmax=1,
xlabel={X},
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=1,
ylabel={Y},
minor tick num=1,
xtick={0,0.1,...,1},
ytick={0,0.1,...,1},
axis background/.style={fill=white},
legend style={at={(0.97,0.03)},anchor=south east,legend cell align=left,align=left,fill=white}
]

\end{axis}

\end{tikzpicture}%
\end{document}
5
  • second one is easy : replace 1 with 1.01 or something larger than 1. The first one I think doesn't work with explicit ticks. Or you need to supply also the minor tick key. I'm speculating though. Didn't check. – percusse Jul 21 '15 at 13:27
  • @percusse It does work with explicit ticks, try xtick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}. Could it have something to with rounding, so that 0,0.1,...,1 doesn't create the exact sequence above? – Torbjørn T. Jul 21 '15 at 13:30
  • @TorbjørnT Thanks I can confirm explicitly setting xtick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0} (and for yticks) solved both problems. Sounds like a bug if 0,0.1,...,1 doesn't work – Mobius Pizza Jul 21 '15 at 13:34
  • 1
    If you want to call floating point roundoff errors (which I would think it is) a bug. – Torbjørn T. Jul 21 '15 at 13:43
  • @TorbjørnT. Yes probably. They don't work with nonuniform sequences. And that 0,...,1 syntax falls back to TikZ and uses TeX arithmetic. So probably that is the culprit. – percusse Jul 21 '15 at 13:53
9

Setting all the ticks explicitly works:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
grid=both,
minor tick num=1,
xtick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0},
ytick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0},
]

\end{axis}

\end{tikzpicture}%
\end{document}

I would think this is caused by roundoff-errors. You can see easily that 0,0.1,...,1 doesn't create the same sequence if numbers as above with

\documentclass{article}
\usepackage{pgffor}

\begin{document}
\foreach \x in {0,0.1,...,1} {
 \x 
}
\end{document}

which outputs

enter image description here

The reason why the 1 tick was missing from both axes is clear. I'm not sure why the minor ticks are missing, it could be because the distances between the major ticks are not all equal, at least the manual states

Minor ticks will be disabled if the major ticks don’t have the same distance and they are currently only available for linear axes (not for logarithmic ones).

3
  • I think the minor ticks are missing because the error introduced by floating point means the distances 0 0.1 0.20001 are somewhat a little bit different, e.g. 0.1 and 0.100001 – Mobius Pizza Jul 21 '15 at 13:53
  • @MobiusPizza You can introduce integers and replace the labels with custom ones if that's a problem for your use case – percusse Jul 21 '15 at 13:54
  • @MobiusPizza My thought exactly, and also what I wrote in my edit. – Torbjørn T. Jul 21 '15 at 13:54
7

You could also try

xtick distance=.1,
ytick distance=.1,

instead of xtick={0,0.1,...,1},ytick={0,0.1,...,1},.

enter image description here

Code:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
footnotesize,
width=4in,
height=4in,
axis equal,
scale only axis,
separate axis lines,
grid=both,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
xmin=0,
xmax=1,
xlabel={X},
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=1,
ylabel={Y},
minor tick num=1,
xtick distance=.1,% <- 
ytick distance=.1,% <-
axis background/.style={fill=white},
legend style={at={(0.97,0.03)},anchor=south east,legend cell align=left,align=left,fill=white}
]

\end{axis}

\end{tikzpicture}%
\end{document}

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.