3

The following MWE

\documentclass[beamer,crop]{standalone}
\usepackage{times}
\usepackage{pgfplots,xparse}

\pgfplotsset{ compat=1.17, % Specify the compatibility version nice_axis/.style={ xlabel={$t/T_c$}, ylabel={$p_{BOC \left( 1,1 \right)}\left( t \right)\cdot \sqrt{T_c}$}, % title={Triangle Function}, grid=both, axis lines=box, samples=101, domain=-2:2, xmin=-1, xmax=1, ymax=1.2, ymin=-1.2, ytick={-1, -0.8, ..., 1}, legend pos=outer north east, } }

\begin{document}

\begin{tikzpicture} \begin{axis}[ nice_axis, ] \addplot[black, thick, mark=none] coordinates { (-1,0) (-0.5,0) (-0.5,1) (0,1) (0,-1) (0.5,-1) (0.5,0) (1,0) }; \end{axis} \end{tikzpicture}

\end{document}

Yields

enter image description here

As far as I could see in in other pots, it is due to roundoff error. How to set ytick so that I don't get the zero-imprecision error? I tried this solution but it seems to work for table only.

  • Could one start at $-5*0.2$ instead of $-1$? – Hagen von Eitzen Nov 01 '23 at 09:04
  • @HagenvonEitzen unfortunately no. Although 0.2 isn't exactly representable, 50.2 rounds to 1.0 exactly. You could say the problem happens in the middle: 0.2 + 0.2 and 0.6 - 0.2 are very* slightly different numbers. – hobbs Nov 01 '23 at 21:17

2 Answers2

6

You can rounded yticklabel by

yticklabel style = {/pgf/number format/fixed},

Complete MWE:

\documentclass[beamer,crop]{standalone}
\usepackage{times}
\usepackage{pgfplots,xparse}

\pgfplotsset{ compat=1.18, % Specify the compatibility version nice_axis/.style={ xlabel={$t/T_c$}, ylabel={$p_{BOC \left( 1,1 \right)}\left( t \right)\cdot \sqrt{T_c}$}, % title={Pulse Function}, grid=both, axis lines=box, domain=-1:1, ytick={-1, -0.8, ..., 1}, yticklabel style = {/pgf/number format/fixed}, xmin=-1, xmax=1, legend pos=outer north east, } }

\begin{document}

\begin{tikzpicture} \begin{axis}[nice_axis] \addplot[thick, mark=none] coordinates { (-1,0) (-0.5,0) (-0.5,1) (0,1) (0,-1) (0.5,-1) (0.5,0) (1,0) }; \end{axis} \end{tikzpicture}

\end{document}

enter image description here

Zarko
  • 296,517
5

enter image description here

You can use \fpeval to round to 2 decimal places.

Just add

        yticklabel={$\fpeval{0 - round(-\tick,2)}$},

The double negation avoids rounding to -0 in this case.

David Carlisle
  • 757,742