35

How can I force pgfplots to draw my axis ticks labels consistently? I mean instead of 0 a 0.0, instead of 1 a 1.0 in the below image.

enter image description here

EDIT: Code

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{siunitx}

\pgfplotsset{
compat=1.7,
every axis/.append style={
line width=1.25pt,
tick style={line width=1.25pt, color=black, line cap=round}
}
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  enlarge x limits=0.05,
  enlarge y limits=0.05,
  try min ticks=4,
  max space between ticks=50pt,
  y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=1
  },
  x tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2
  }
]
\addplot[red] table[x index=1,y index=7] {dataA.dat};
\end{axis}
\end{tikzpicture}

\end{document}

I can't attach the data file, so here's a link.

1 Answer 1

56

You can change the number format using the /pgf/number format keys (there are a lot of options). In this concrete case, you'll want to set fixed, fixed zerofill, precision=1 for the y axis, and precision=2 for the x axis:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}

\begin{axis}[
    y tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=1,
        /tikz/.cd
    },
    x tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
        /tikz/.cd
    }
]
\addplot [domain=0.98:1.02] {rnd};
\end{axis} 
\end{tikzpicture}

\end{document}
6
  • I'll get an error. This is, I guess, due to the numbers are very small. They have the expansion 1e-19 or something. Is there a way to deal with that?
    – DaPhil
    Feb 1, 2013 at 16:17
  • @DaPhil: Sorry, I forgot to change the key directory back using /tikz/.cd (PGFPlots was still trying to apply normal tikz options using the /pgf/number format directory. I've edited my answer.
    – Jake
    Feb 1, 2013 at 16:40
  • 1
    I took the liberty of adding a comma , after precision=2.
    – user11232
    Feb 1, 2013 at 23:51
  • @Jake: When and why do we need the change directory key /tikz/.cd? The manual (v1.9 §4.4/p.74) states something else...
    – strpeter
    Jan 9, 2014 at 8:48
  • 3
    @strpeter: You need to change the directory key back using /tikz/.cd if you later want to apply other options to the labels that don't belong to the /pgf/number format directory (e.g. adding x tick label style=red later on). That's not the case in this code example, but resetting the directory to /tikz anyway can't hurt.
    – Jake
    Jan 9, 2014 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .