使用 Tikz 绘制不同的正弦波而不绘制零点

使用 Tikz 绘制不同的正弦波而不绘制零点

我需要绘制各种正弦波,并使图形看起来几乎完全符合我的要求。但是,我无法删除 0π 或使其看起来像 3π/2。

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
  \pgfkeys{/pgf/number format/precision=2}
  \pgfmathparse{#1/pi}
  \pgfmathroundtozerofill{\pgfmathresult}
  \pgfmathifisint{\pgfmathresult}{
    \pgfmathprintnumber{\pgfmathresult}$\pi$%}
  }{
    \pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
  }
}

\begin{document}
  \begin{tikzpicture}
    \datavisualization [
      school book axes,
      all axes={
        grid={
          major={style={red!50!black, opacity=.25}},
          minor={style={green!25!black, opacity=.25}},
          minor steps between steps=3,
      }},
      x axis={
        label=$\phi$,
        ticks and grid={
          stack,
          step=(2*pi),
          tick typesetter/.code=\mytypesetter{##1},
      }},
      y axis={
        label=$v$,
        grid={step=1}
      },
      style sheet=vary hue,
      visualize as line/.list={sin1}
    ]
    data [set=sin1, format=function] {
      var x : interval [-2*pi:2*pi] samples 100;
      func y = sin(\value x/2 r);
    };
  \end{tikzpicture}
\end{document}

答案1

欢迎!您可以0\pi按如下方式关闭。

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
  \pgfkeys{/pgf/number format/precision=2}
  \pgfmathparse{#1/pi}
  \pgfmathroundtozerofill{\pgfmathresult}
  \pgfmathifisint{\pgfmathresult}{\pgfmathtruncatemacro{\itest}{\pgfmathresult}%
  \unless\ifnum\itest=0
    \pgfmathprintnumber{\pgfmathresult}$\pi$%
  \fi
  }{
    \pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
  }
}

\begin{document}
  \begin{tikzpicture}
    \datavisualization [
      school book axes,
      all axes={
        grid={
          major={style={red!50!black, opacity=.25}},
          minor={style={green!25!black, opacity=.25}},
          minor steps between steps=3,
      }},
      x axis={
        label=$\phi$,
        ticks and grid={
          stack,
          step=(2*pi),
          tick typesetter/.code=\mytypesetter{##1},
      }},
      y axis={
        label=$v$,
        grid={step=1}
      },
      style sheet=vary hue,
      visualize as line/.list={sin1}
    ]
    data [set=sin1, format=function] {
      var x : interval [-2*pi:2*pi] samples 100;
      func y = sin(\value x/2 r);
    };
  \end{tikzpicture}
\end{document}

在此处输入图片描述

或者是抑制量级因素的变体1

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
  \pgfkeys{/pgf/number format/precision=2}
  \pgfmathparse{#1/pi}
  \pgfmathroundtozerofill{\pgfmathresult}
  \pgfmathifisint{\pgfmathresult}{\pgfmathtruncatemacro{\itest}{\pgfmathresult}%
  \unless\ifnum\itest=0
    \ifnum\itest=1
     $\pi$%
    \else
     \ifnum\itest=-1
      $-\pi$%
     \else
      \pgfmathprintnumber{\pgfmathresult}$\pi$%
     \fi
    \fi
  \fi
  }{
    \pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
  }
}

\begin{document}
  \begin{tikzpicture}
    \datavisualization [
      school book axes,
      all axes={
        grid={
          major={style={red!50!black, opacity=.25}},
          minor={style={green!25!black, opacity=.25}},
          minor steps between steps=3,
      }},
      x axis={
        label=$\phi$,
        ticks and grid={
          stack,
          step=(pi),
          tick typesetter/.code=\mytypesetter{##1},
      }},
      y axis={
        label=$v$,
        grid={step=1}
      },
      style sheet=vary hue,
      visualize as line/.list={sin1}
    ]
    data [set=sin1, format=function] {
      var x : interval [-2*pi:2*pi] samples 100;
      func y = sin(\value x/2 r);
    };
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容