Graphics, Figures & TablesRound Precision for Axis Labels

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
computernormal
Posts: 3
Joined: Tue Aug 27, 2013 1:01 am

Round Precision for Axis Labels

Post by computernormal »

Hello,

how can I prevent, that the values on my axis were rounded on two digits after point? I need three digits after the point.

Code: Select all

\begin{tikzpicture}
  \begin{axis}[
    legend pos=outer north east,
    grid=major,
    xlabel=$t\;in\;ms$,
    ytick={-0.625,-0.5,-0.375,-0.25,-0.125,0,0.125,0.25,0.375,0.5,0.625},
    ylabel=$U\;in\;Volt$, xmin=0,
  ]
    \addplot[smooth,color=blue,mark=none] coordinates {
      (0,-0.5)
      (3,-0.5)
    };
    \addplot[smooth,color=blue,mark=none] coordinates {
      (0,-0.375)
      (3,-0.375)
    };
    \addplot[smooth,color=blue,mark=none,fill opacity=.2] coordinates {
      (0,-0.25)
      (3,-0.25)
    };
    \addplot[smooth,color=blue,mark=none,fill opacity=.2] coordinates {
      (0,-0.125)
      (3,-0.125)
        };      
    \addplot[smooth,color=blue,mark=none,fill opacity=.2] coordinates {
      (0,0)
      (3,0)
    };
    \addplot[smooth,color=blue,mark=none,fill opacity=.2] coordinates {
      (0,0.125)
      (3,0.125)
    };
    \addplot[smooth,color=blue,mark=none,fill opacity=.2] coordinates {
      (0,0.25)
      (3,0.25)
    };
    \addplot[smooth,color=blue,mark=none,fill opacity=.2] coordinates {
      (0,0.375)
      (3,0.375)
    };      
    \addplot[smooth,color=blue,mark=none,fill opacity=.2] coordinates {
      (0,0.5)
      (3,0.5)
    };                               
    \addplot[smooth,color=red,mark=x] coordinates {
      (0,0)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (0.2,0)
      (0.2,0.4)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (0.4,0)
      (0.4,0.458)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (0.6,0)
      (0.6,-0.08)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (0.8,0)
      (0.8,-0.447)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (1,0)
      (1,0)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (1.2,0)
      (1.2,0.4)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (1.4,0)
      (1.4,0.33)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (1.6,0)
      (1.6,0)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (1.8,0)
      (1.8,-0.325)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (2,0)
      (2,-0)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (2.2,0)
      (2.2,0.435)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (2.4,0)
      (2.4,0.335)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (2.6,0)
      (2.6,-0.1)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (2.8,0)
      (2.8,-0.48)
    };
    \addplot[smooth,color=red,mark=x] coordinates {
      (3,0)
      (3,0)
    };
    \legend{$f[t]$};
  \end{axis}
\end{tikzpicture}

greetz andre

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Round Precision for Axis Labels

Post by localghost »

You need to explicitly specify the number format for the ticks by the yticklabel style key. Furthermore I ventured to shorten your example by making it a comb plot in y direction and completed it to make it compilable. For details please refer to the pgfplots manual. The output is attached.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
  compat=newest,
  major grid style=blue,
  xlabel near ticks,
  ylabel near ticks
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      legend pos=outer north east,
      xmin=0,
      xmax=3,
      xlabel={$t$ / ms},
      ymajorgrids=true,
      ymin=-0.625,
      ymax=0.625,
      ytick={-0.625,-0.5,...,0.625},
      yticklabel style={/pgf/number format/.cd,fixed,precision=3},
      ylabel={$U$ / V}
    ]
      \addplot[ycomb,color=red,mark=x] coordinates {
        (0,0)
        (0.2,0.4)
        (0.4,0.458)
        (0.6,-0.08)
        (0.8,-0.447)
        (1,0)
        (1.2,0.4)
        (1.4,0.33)
        (1.6,0)
        (1.8,-0.325)
        (2,0)
        (2.2,0.435)
        (2.4,0.335)
        (2.6,-0.1)
        (2.8,-0.48)
        (3,0)
      };
      \legend{$f[t]$};
    \end{axis}
  \end{tikzpicture}
\end{document}
Remarks:
  • For physical quantities you may look at the siunitx package.

Thorsten
Attachments
otmp.png
otmp.png (4.87 KiB) Viewed 10888 times
Post Reply